编写自己的xxx-spring-boot-starter

1.引入两个包

<dependencies>
<!--负责从properties文件中读取配置信息-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>2.1.5.RELEASE</version>
  </dependency>
<!--实现自动化配置-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.1.3.RELEASE</version>
  </dependency>
</dependencies>

2.编写配置类

配置文件读取类 xxxProperties

@ConfigurationProperties(prefix = "com.satrry")
@Data
public class StarryServiceProperties {
    // 自己需要的配置和yml配置文件中属性对应 eg:
    public static final String DefaultAppName = "yyds"
    private String appName = DefaultAppName;
    .....
}

那么配置文件就是

com.satrry.appName = xxx 

3.编写自己需要集成进starter中的服务

4. 编写自动装配类 xxxAutoConfigure

指定发现classpath下有xxx.class的话,进行自动配置

@Configuration
@ConditionalOnClass(xxx.class)
@EnableConfigurationProperties(StarryServiceProperties.class)
public class XxxAutoConfigure {
    @Autowired
    private StarryServiceProperties properties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "com.starry", value = "enabled", havingValue = "true")
    StarterService starterService (){
        return new StarterService(properties.getConfig());
    }

}

5.新建spring.factories文件

resources/META-INF/ 路径下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.starry.StarryAutoConfigure

6.打包

mvn install

这里埋下一个注意点。自行百度starter mvn 打包注意。