<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>
配置文件读取类 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
略
指定发现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());
}
}
resources/META-INF/ 路径下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.starry.StarryAutoConfigure
mvn install
这里埋下一个注意点。自行百度starter mvn 打包注意。
版权属于:dingzhenhua
本文链接:https://cloud.tencent.com/developer/article/2019222
转载时须注明出处及本声明