自定义 Spring Boot Starter

自定义 Spring Boot Starter

参考自:www.wanyj.cn/article/cus...

1. 命名规范

  • 官方的命名方式:spring-boot-starter-xxx
  • 第三方命名方式:xxx-spring-boot-starter

2. 功能开发

2.1. 引入基础依赖

xml 复制代码
<!-- Spring Boot Starter基础依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 自动配置核心依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 用于生成配置元数据,提供IDE提示支持 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
<!-- 其他你需要的依赖 -->

2.2. 编写配置信息类

java 复制代码
import cn.wanyj.component.dcc.types.common.Constants;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "wanyj.component.dcc", ignoreInvalidFields = true)
public class DynamicConfigCenterAutoConfigProperties {

    private String system;

    public String getFinalAttributeName(String attributeName) {
        return this.system + Constants.UNDERLINE + attributeName;
    }

    public String getSystem() {
        return system;
    }

    public void setSystem(String system) {
        this.system = system;
    }
}

这样你就可以在yml文件中配置了

yml 复制代码
wanyj:
  component:
    dcc:
      system: test

2.3. 完成业务逻辑,编写自动配置类

java 复制代码
import cn.wanyj.component.dcc.domain.entity.AttributeEntity;
import cn.wanyj.component.dcc.domain.service.DynamicConfigCenterService;
import cn.wanyj.component.dcc.domain.service.DynamicConfigCenterServiceImpl;
import cn.wanyj.component.dcc.listener.DynamicConfigCenterAdjustListener;
import cn.wanyj.component.dcc.types.common.Constants;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(value = DynamicConfigCenterAutoConfigProperties.class)
public class DynamicConfigCenterRegisterAutoConfig {

    @Bean
    public DynamicConfigCenterService dynamicConfigCenterService(DynamicConfigCenterAutoConfigProperties properties, RedissonClient redissonClient) {
        return new DynamicConfigCenterServiceImpl(properties, redissonClient);
    }

    @Bean
    public DynamicConfigCenterAdjustListener dynamicConfigCenterAdjustListener(DynamicConfigCenterService dynamicConfigCenterService) {
        return new DynamicConfigCenterAdjustListener(dynamicConfigCenterService);
    }

    @Bean
    public RTopic dynamicConfigCenterRedisTopic(DynamicConfigCenterAutoConfigProperties properties, RedissonClient redissonClient, DynamicConfigCenterAdjustListener dynamicConfigCenterAdjustListener) {
        RTopic topic = redissonClient.getTopic(Constants.getTopic(properties.getSystem()));
        topic.addListener(AttributeEntity.class, dynamicConfigCenterAdjustListener);
        return topic;
    }
}

2.4. 使starter生效

  • 方式一:注解启动

你需要自定义一个启动注解

java 复制代码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({DynamicConfigCenterRegisterAutoConfig.class}) // [!code highlight]
public @interface EnableDCC {
}

然后使用时需要在启动类添加这个注解

java 复制代码
@SpringBootApplication
@EnableDCC // [!code highlight]
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • 方式二:自动生效
    • 通过 SpringBoot 的 SPI 的机制来去加载我们的 Starter
    • 需要创建resource/META-INF/spring.factories文件
      • key 为 org.springframework.boot.autoconfigure.EnableAutoConfiguration
      • value 为自动配置类的全限定名(记得去除前后的空格,否则会不生效)
[spring.factories] 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.wanyj.component.dcc.config.DynamicConfigCenterRegisterAutoConfig

!WARNING\] 注意 在 Springboot3+ 版本完全移除了 spring.factories 的方式 需要更改目录结构为 `resource/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports` ```[org.springframework.boot.autoconfigure.AutoConfiguration.imports] cn.wanyj.component.dcc.config.DynamicConfigCenterRegisterAutoConfig ```

相关推荐
用户40993225021229 分钟前
Vue3中v-if与v-for为何不能在同一元素上混用?优先级规则与改进方案是什么?
前端·vue.js·后端
blurblurblun35 分钟前
Go语言特性
开发语言·后端·golang
Y.O.U..36 分钟前
Go 语言 IO 基石:Reader 与 Writer 接口的 “最小设计” 与实战落地
开发语言·后端·golang
冒泡的肥皂39 分钟前
25年AI我得DEMO老师
人工智能·后端
茹鲸40 分钟前
我开发了一个文件智能分类工具,彻底解决了桌面文件杂乱的问题
后端
思成Codes43 分钟前
Gin 框架:*gin.Engine 主要方法
后端·golang·gin
举大栗子1 小时前
Hikari数据库连接池部分常用参数解析
后端
回家路上绕了弯1 小时前
分布式事务SAGA模式详解:长事务与复杂流程的柔性事务方案
分布式·后端
GreatSQL1 小时前
一次由隐藏大页配置引发的数据库OOM故障分析
后端
码界奇点1 小时前
基于Spring Boot的后台管理系统设计与实现
java·spring boot·后端·车载系统·毕业设计·源代码管理