一、背景介绍
Springboot Starter机制抛弃了过去创建一个Spring项目需要依赖大量繁琐的jar包和配置信息,同时也规避了版本冲突的问题,做Java开发的经历中一定碰到过引入各种依赖出现版本冲突的噩梦,Starter的出现就是为了规避这样的问题,做到同一个依赖的版本统一。本文主要说明Starter的原理和如何自定义一个Starter启动器。
二、自定义Starter
springboot starter利用自动装载的原理,将starter中的配置项自动加载到IoC容器中,降低配置的复杂性。自定义一个starter的主要步骤为:
- 引入POM依赖;
- 配置和配置文件对应的xxxProperties类;
- 配置业务类;
- 配置自动配置xxxAutoConfiguration类;
- 配置spring.factories文件;
1.配置POM文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
其中spring-boot-autoconfigure是必须要引入的。
2.定义属性配置类xxxProperties
@ConfigurationProperties(prefix = "com.test")
public class MyStarterProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MyStarterProperties这个文件的属性和配置文件中的配置项是对应的,@ConfigurationProperties(prefix = “com.test”)表示该配置项的key以com.test为前缀。
3.定义自动配置类XXXAutoConfiguration
@EnableConfigurationProperties({
MyStarterProperties.class,
})
@Configuration
public class MyStarterAutoConfiguration {
@Bean
MyStarterTemplate getMyStarterTemplate(MyStarterProperties myStarterProperties) {
return new MyStarterTemplate(myStarterProperties);
}
}
MyStarterAutoConfiguration是自动配置类,他会自动加载MyStarterProperties属性配置类,并且会返回MyStarterTemplate这个业务相关的配置类,这个类是和业务相关的,需要我们自己实现。
4.定义业务Bean类
public class MyStarterTemplate {
MyStarterProperties myStarterProperties;
public MyStarterTemplate(MyStarterProperties myStarterProperties) {
this.myStarterProperties = myStarterProperties;
}
public void printName() {
System.out.println("myStarterProperties.getName() = " + myStarterProperties.getName());
}
}
MyStarterTemplate这个配置类也会被自动加载。
5.定义spring.factories文件
在resources/META-INF创建一个spring.factories文件和spring-configuration-metadata.json文件。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yangnk.threadpoolstarter.myStarter.MyStarterAutoConfiguration
{
"group": [
{
"name": "com.yangnk",
"type": "com.yangnk.threadpoolstarter.config.MyStarterProperties",
"sourceType": "com.yangnk.threadpoolstarter.config.MyStarterProperties"
}
],
"properties": [
{
"name": "com.yangnk.name",
"type": "java.lang.String",
"description": "my start name",
"sourceType": "com.yangnk.threadpoolstarter.myStarter.MyStarterProperties",
"defaultValue": "MyStarterProperties name"
}
]
}
如果不想要手动实现pring-configuration-metadata.json,引入的spring-boot-configuration-processor依赖会帮我们自动生成。
6.打包测试
通过maven的install命令就可以将该依赖打包到本地仓库。在测试的项目中引入该依赖即可,在Properties配置文件中加上配置项。
三、代码实现
myStarterDemo完整代码:https://github.com/yangnk/SpringBoot_Learning/tree/master/MyStarterDemo
参考资料
- 手把手教你实现自定义 Spring Boot 的 Starter:https://xie.infoq.cn/article/68621c5f5c1dc16312e0a52e4
- SpringBoot自动装配、启动流程及自定义starter:https://juejin.cn/post/7174031833183551519#heading-3
- SpringBoot2.x系列教程60–SpringBoot如何自定义Starter启动器?:https://juejin.cn/post/7180097458351898682
- 自定义springboot starter,你学废了吗:https://juejin.cn/post/7127468724046528525
- SpringBoot SPI 机制和实现自定义 starter:SpringBoot SPI 机制和实现自定义 starter:https://juejin.cn/post/7132132528810852382
- :大聪明教你学Java | 深入浅出聊 SpringBoot 中的 starter 机制:https://juejin.cn/post/7242815848087978040