一个starter需要什么?
通过分析springboot中官方定义的众多starter,我们可以得出,一个starter需要具备两大方面:
1.这个starter应该配置的依赖有哪些;
2.怎样实现自动配置。
starter结构及命名规则:
启动器模块是一个空jar文件,仅提供辅助性依赖管理,这些依赖可用于自动装配或者其他类库;
结构为:xxx-starter,xxx-starter-autoconfigurer。
自定义命名规则:xxx-spring-boot-starter,例如:mybatis-spring-boot-starter。
需要用到的注解:
1.Configuration:表示自己是一个配置类
2.ConditionalOnXxx:判断条件成立的情况下自动配置生效
3.AutoConfigureAfter:设置自动配置顺序,在注解参数类执行之后执行
4.AutoConfigureOrder:设置自动配置顺序,参数值越小,优先执行度越高。
5.Bean:把组件导入容器中
6.ConfigurationProperties:结合相关XxxProperties类来绑定相关的配置
7.EnableConfigurationProperties:把XxxProperties类导入到容器中
ps:务必将需要启动就加载的自动配置类,配置到META-INF/spring.factories中
具体实现自定义starter过程:
1.创建一个空项目,在这个空项目中创建两个模块,一个maven工程xxx-starter,一个sprignboot工程xxx-starter-autoconfigurer。
2.xxx-starter只是起辅助性依赖管理作用,只需要在pom.xml中引入xxx-starter-autoconfigurer的groupId、artifactId、version信息即可。
<groupId>com.codetree.starter</groupId>
<artifactId>tree-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
3.往xxx-starter-autoconfigurer中引入spring-boot-starter依赖,这是所有starter都必须引入的基本依赖
<dependencies>
<!--引入springboot-starter,所有starter都必须要引入的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
4.创建一个HelloService,编写一个简单输出方法
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix()+"--"+name + helloProperties.getSuffix();
}
}
5.创建HelloProperties ,两个成员变量(前缀后缀)用于后续验证,加上ConfigurationProperties注解,指定配置文件前缀。
@ConfigurationProperties(prefix = "tree.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
6.创建HelloServiceAutoConfiguration自动配置类,
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
7.在resources资源文件夹下创建一个META-INF文件夹,在这个文件夹下创建spring.factories文件。在其中加入以下内容,这样主程序启动之后才能被自动装配,导入到容器中。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.codetree.starter.HelloServiceAutoConfiguration
8.使用maven功能,先install xxx-starter-autoconfigurer,然后install xxx-starter
9.测试:
创建一个springboot工程,在pom.xml中导入xxx-starter依赖,在主配置文件中配置自动义启动器的前缀后缀,然后写一个controller就能测试了。
<dependency>
<groupId>com.codetree.starter</groupId>
<artifactId>tree-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
tree.hello.prefix=CODETREE
tree.hello.suffix=HELLO WORLD
@RestController//返回json数据的controller
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String hello(){
return helloService.sayHello("fxy");
}
}
测试结果:
starter总结:
springboot主程序启动时,会加载主配置类,开启自动配置功能,自动配置底层会去遍历META-INF/spring.factories文件,然后将其中的组件导入容器,在这个例子中就是HelloServiceAutoConfiguration会被导入容器,然后根据HelloServiceAutoConfiguration这个自动配置类的产生作用条件是否符合,来决定是否开启这个类的自动装配机制,如果成立则先加载HelloProperties.class进容器,在HelloProperties中,它的@ConfigurationProperties(prefix = “tree.hello”)注解会从配置文件中将以“tree.hello”为前缀的信息绑定到成员变量中,然后HelloService获取时就可以成功取到值了。