自定义SpringBootStarter

186 阅读2分钟

介绍

自定义Starter主要用于实现Bean的自动装配,只要引入依赖就可以实现Bean的自动注入,不需要引入者手动注入Bean。

基本实现

  1. 准备自动装配的配置类,一般取名为XxxAutoConfiguration

  2. 将需要实现自动装配的Bean在XxxAutoConfiguration配置类内声明

    可以使用@Import注解直接引入,也可以用@Bean的方式声明

    [!TIP] 对于不需要入参,不需要条件判断的,建议直接使用@Import注入的方式更加快捷

    对于需要入参或者需要使用条件判断的Bean,必须使用@Bean的方式进行注入

    如下案例

    /**
     * 自动装配配置类
     *
     * @author Klaus
     */
    @Import({AIoTServerProperties.class, AIoTMqttProperties.class})
    public class AIoTClientAutoConfiguration {
    
        @Bean
        @ConditionalOnProperty(prefix = AIoTServerProperties.PREFIX, name = "host")
        public AIoTClientApplicationRunner aiotClientApplicationRunner(AIoTServerProperties aiotServerProperties) {
            return new AIoTClientApplicationRunner(aiotServerProperties);
        }
    
        @Bean
        @ConditionalOnProperty(prefix = AIoTClientSubscribeProperties.PREFIX, name = "device-data", havingValue = "true")
        public AIoTClientDeviceDataMqttClient aiotDeviceDataMqttClient(AIoTMqttProperties aiotMqttProperties,
                                                                       AIoTClientSubscribeProperties aiotClientSubscribeProperties) {
            return new AIoTClientDeviceDataMqttClient(aiotMqttProperties, aiotClientSubscribeProperties);
        }
    
    }
    
  3. 创建工厂配置文件

    • SpringBoot < 2.7

      在starter的resources目录下创建META-INF文件夹,在该文件夹下创建文件spring.factories,并在spring.factories文件的第一行写入如下内容

      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
      
    • SpringBoot >= 2.7

      SpringBoot在2.7版本更新以后,更新了自动装配的配置文件路径和名称

  4. 声明需要自动装配的类

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.iot.msglistener.config.AIoTClientAutoConfiguration
    

    如有多个类配置类需要实现自动装配

    1. 可以在AIoTClientAutoConfiguration类内声明

    2. 可以直接像AIoTClientAutoConfiguration一样声明在spring.factories文件

      多行使用,\后换行

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.iot.msglistener.config.AIoTClientAutoConfiguration,\
    com.iot.msglistener.config.BConfiguration
    
  5. 实现Properties配置提示

    可以看到我们前面有注入两个配置类@Import({AIoTServerProperties.class, AIoTMqttProperties.class})

    我们希望引用该starter的开发者,在编写该Properties的配置时,可以有提示并且可以跳转到对应的Properties类

    在starter的pom文件内引入依赖spring-boot-configuration-processor即可

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
    

    该starter打成的jar包的META-INF目录下会自动生成spring-configuration-metadata.json文件,该文件定义了所有Properties类的格式,格式如下(内容仅供参考)

    {
      "groups": [
        {
          "name": "aiot.build",
          "type": "com.iot.dev.properties.BuildProductProperties",
          "sourceType": "com.iot.dev.properties.BuildProductProperties"
        }
      ],
      "properties": [
        {
          "name": "aiot.build.broadcast-enabled",
          "type": "java.lang.Boolean",
          "description": "emqx内设备数据推送方式改为广播模式",
          "sourceType": "com.iot.dev.properties.BuildProductProperties",
          "defaultValue": false
        }
      ],
      "hints": []
    }