创建自己的Starter
springboot 官方提供了很多 starter 来实现自动配置,简化我们的开发,那么我们能不能把自己常用的一些组件也封装成一个starter来实现自动化配置呢?答案是可以的。
自定义Starter
1. 创建一个空的 maven 项目,并引入依赖
<dependencies>
<!-- 自定义starter必须引入的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<!-- application.properties参数提示依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
</dependencies>
2. 创建配置属性类
@ConfigurationProperties("mystarter")
public class MystarterProperties {
private String pro1;
private String pro2;
private String pro3;
// 设置默认值
private Integer pro4 = 1000;
public String getPro1() {
return pro1;
}
public void setPro1(String pro1) {
this.pro1 = pro1;
}
public String getPro2() {
return pro2;
}
public void setPro2(String pro2) {
this.pro2 = pro2;
}
public String getPro3() {
return pro3;
}
public void setPro3(String pro3) {
this.pro3 = pro3;
}
public Integer getPro4() {
return pro4;
}
public void setPro4(Integer pro4) {
this.pro4 = pro4;
}
}
3. 创建服务类
public class MystarterService {
private String pro1;
private String pro2;
private String pro3;
private Integer pro4;
@Override
public String toString() {
return "MystarterService{" +
"pro1='" + pro1 + '\'' +
", pro2='" + pro2 + '\'' +
", pro3='" + pro3 + '\'' +
", pro4=" + pro4 +
'}';
}
public MystarterService(String pro1, String pro2, String pro3, Integer pro4) {
this.pro1 = pro1;
this.pro2 = pro2;
this.pro3 = pro3;
this.pro4 = pro4;
}
public String getPro1() {
return pro1;
}
public void setPro1(String pro1) {
this.pro1 = pro1;
}
public String getPro2() {
return pro2;
}
public void setPro2(String pro2) {
this.pro2 = pro2;
}
public String getPro3() {
return pro3;
}
public void setPro3(String pro3) {
this.pro3 = pro3;
}
public Integer getPro4() {
return pro4;
}
public void setPro4(Integer pro4) {
this.pro4 = pro4;
}
}
4. 创建自动配置类
@Configuration
@EnableConfigurationProperties({MystarterProperties.class}) // 开启配置
public class MystarterAutoConfiguration {
@Autowired
MystarterProperties mystarterProperties;
@Bean
@ConditionalOnMissingBean // 当前类不存在,开始注入
public MystarterService getMystarterService() {
return new MystarterService(mystarterProperties.getPro1(), mystarterProperties.getPro2(), mystarterProperties.getPro3(), mystarterProperties.getPro4());
}
}
5. 创建 META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lizq.mystarter.MystarterAutoConfiguration
6. 创建 META-INF/additional-spring-configuration-metadata.json
SpringBoot 通过 spring-boot-configuration-processor maven 依赖结合 additional-spring-configuration-metadata.json 文件,来控制在 application.properties 中的配置自定义start组件的提示信息。additional-spring-configuration-metadata.json文件内容格式如下:
{
"properties": [
{
"name": "mystarter.pro1",
"type": "java.lang.String",
"description": "自定义properties参数描述",
"defaultValue": "默认值1"
},
{
"name": "mystarter.pro2",
"type": "java.lang.String",
"description": "自定义properties参数描述",
"defaultValue": "默认值2"
},
{
"name": "mystarter.pro3",
"type": "java.lang.String",
"description": "自定义properties参数描述",
"defaultValue": "默认值3"
},
{
"name": "mystarter.pro4",
"type": "java.lang.Integer",
"description": "自定义properties参数描述",
"defaultValue": 3000
}
]
}
测试Starter
新建一个 springboot 项目来测试一下我们自定义的starter。在 pom.xml 中引入依赖。
<dependency>
<groupId>com.lizq</groupId>
<artifactId>mystarter-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
1.测试 MystarterService 服务类是否自动装配
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
ApplicationContext ac = SpringApplication.run(DemoApplication.class, args);
}
@Autowired
private MystarterService service;
@Override
public void run(String... args) throws Exception {
System.out.println(service.toString());
}
}
打印结果:
2.测试配置文件是否生效
在 application.yml 中添加配置,如下:
mystarter:
pro1: pro1
pro3: pro3
pro4: 3000
启动,打印结果为:
至此,我们完成了自定义starter,并且测试成功。关于SpringBoot的核心原理,可以查看 《SpringBoot 核心原理》