自定义starter
starter:
1、这个场景需要的依赖是什么?
2、如何编写自动配置
启动流程
1、创建SpringApplication对象
2、如何编写自动配置
@Configuration//指定这是一个配置类
@ConditionalOnXXX//指定条件成了的情况下自动配置类生效
@AutoConfigureOrder//指定自动配置类的顺序
@Bean//在容器中添加组件
@ConfigurationProperties//结合相关的XXXProperties类来绑定相关的配置
@EnableConfigurationProperties//让XXXproperties类生效,并且加入到容器中
//自动配置类要能加载,需要启动就加载到自动配置类中META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
3、模式
启动器(starter)只用来做以来导入:专门用来写一个自动配置模块,启动器依赖自动配置,其他人只需要引入启动器就ok了。
自定义命名:
- -后缀:“*-spring-boot-starter”
- -模式:“*-spring-boot-starter”
实际操作
1、首先我们新建一个空项目
2、在这个空项目里添加modules【模块】
3、先添加一个maven模块
4、再添加一个springboot模块
5、完成两个模块的添加
6、开始编码
最基础的东西我们需要新增以下几个文件
HelloProperties
定义了Properties的配置项的内容,前缀需要用atlyb.hello来启用
@ConfigurationProperties(prefix = "atlyb.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;
}
}
HelloService
public class HelloService {
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
HelloProperties helloProperties;
public String sayHello(String name)
{
return helloProperties.getPrefix()+"-"+ name+"-"+helloProperties.getSuffix();
}
}
HelloServiceAutoConfiguration
@EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效
也可以在@ConfigurationProperties 使用 @Component 注册,不过相对麻烦。
如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。
@Configuration
@ConditionalOnWebApplication//如果是web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
编写autoconfigure的spring.properties工厂
注意此处必须是META-INF下的spring.properties文件夹,在一开始的时候说明过,自动注入的原理就是在寻找这个路径下的配置注入信息。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.atlyb.starter.HelloServiceAutoConfiguration
starter的pom中引入autoconfigure的包
<!--启动器-->
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.atlyb.starter</groupId>
<artifactId>atlyb-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
=============================================================================================
7、以上几步就完成了一个简单的自定义starter接下来我们通过maven的lifecircle中的install来安装,要按照依赖顺序安装,首先是autoconfigure然后是starter
可以看到就装到了我们的本地库
8、编写测试
我们新建一个springboot工程。把刚才的生成的maven包引入。
写一个controller方法
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.sayHello("habi");
}
}
之前我们定义的前后的方法都是通过配置加入的。
9、在properties方法中写入我们的配置
atlyb.hello.prefix= goodpre
atlyb.hello.suffix= goodsuf
10、运行起来
自定义starter完成