在开发过程当中咱们就常常使用到各类starter,好比mybatis-spring-boot-starter,只须要进行简单的配置便可使用,就像一个插件很是方便。这也是SpringBoot很是重要的一个特性——自动化配置。php
命名规范: Spring官方的Starter命名格式通常是spring-boot-starter-{name},好比spring-boot-starter-web 。而非官方的,官方建议artifactId命名应该遵循 {name}-spring-boot-starter的格式,如example-spring-boot-starter。html
pom文件java
spring-boot-configuration-processor 的做用是编译时生成 spring-configuration-metadata.json ,此文件主要给IDE使用,ctlr+鼠标左键点击配置文件(如application.properties)上相关配置属性,便可跳转到配置此属性的类中。git
咱们要实现的一个小功能是读取配置文件上cn.sp.config的字符串,而后按照给定的分隔符进行分割。github
@ConfigurationProperties(prefix = "cn.sp")
public class StarterServiceProperties {
private String config;
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
}
}
public class StarterService {
private String config;
public StarterService(String config){
this.config = config;
}
public String[] split(String separatorChar){
return this.config.split(separatorChar);
}
}
说下这几个注解的做用:web
下面列举SpringBoot中的全部@Conditional注解及做用spring
@ConditionalOnBean:当容器中有指定的Bean的条件下
@ConditionalOnClass:当类路径下有指定的类的条件下
@ConditionalOnExpression:基于SpEL表达式做为判断条件
@ConditionalOnJava:基于JVM版本做为判断条件
@ConditionalOnJndi:在JNDI存在的条件下查找指定的位置
@ConditionalOnMissingBean:当容器中没有指定Bean的状况下
@ConditionalOnMissingClass:当类路径下没有指定的类的条件下
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件下
@ConditionalOnProperty:指定的属性是否有指定的值
@ConditionalOnResource:类路径下是否有指定的资源
@ConditionalOnSingleCandidate:当指定的Bean在容器中只有一个,或者在有多个Bean的状况下,用来指定首选的Bean
@ConditionalOnWebApplication:当前项目是Web项目的条件下apache
在 resources/META-INF/ 文件夹下建立spring.factories文件,内容以下:json
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.sp.autoconfigure.StarterAutoConfigurebash
右边的就是自动配置类的类路径,注意单词别打错了,我就是META-INF打成了MATA-INF害我折腾了半天。
<dependency>
<groupId>cn.sp</groupId>
<artifactId>example-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
而后能够看到jar包的结构图以下:
3. 在application.properties文件添加以下内容
cn.sp.enabled=true
cn.sp.config=fdafdf,ss1,DSDS,DDD
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySpringbootApplicationTests {
@Autowired
StarterService starterService;
@Test
public void contextLoads() {
String[] strings = starterService.split(",");
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
}
}
2019-05-23 10:41:49.219 [main] INFO cn.sp.MySpringbootApplicationTests - Started MySpringbootApplicationTests in 10.977 seconds (JVM running for 13.035)
fdafdf
ss1
DSDS
DDD
2019-05-23 10:41:52.411 [Thread-4] INFO o.s.w.c.s.GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@51f49060: startup date [Thu May 23 10:41:38 CST 2019]; root of context hierarchy
1.在应用程序启动过程当中,Spring Boot使用SpringFactoriesLoader类加载器查找org.springframework.boot.autoconfigure.EnableAutoConfiguration关键字对应的Java配置文件。Spring Boot会遍历在各个jar包中META-INF目录下的spring.factories文件,构建成一个配置文件链表。
2.根据spring.factories配置加载AutoConfigure类
3.根据 @Conditional注解的条件,进行自动配置并将Bean注入Spring Context中。
注意: Spring Boot的starter在编译时不须要依赖Spring Boot的库。
代码地址:https://github.com/2YSP/example-spring-boot-starter
参考:
https://juejin.im/entry/58d37630570c350058c2c15c
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html