1. 官方文档
SpringBoot 版本 2.6.13,相关链接 Developing with Spring Boot
1.1 什么是 Starter
Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access,include the spring-boot-starter-data-jpa
dependency in your project
The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.
1.2 Starter 命名规范
- 官方 starter:spring-boot-starter-*
- 第三方 starter:*-spring-boot-starter
2. 自定义 starter
starter 最令人津津乐道的就是其自动配置特性,我们自定义一个 starter,演示一下该功能。我的上一篇博文,分析了 SpringBoot 的自动配置原理,有兴趣的小伙伴可以移步阅读。
2.1 相关代码准备
2.1.1 创建实体类 UserProperties
@Data
@ConfigurationProperties(prefix = "com.ys.prop")
public class UserProperties {
private String version = "1.0";
private User user;
@Data
public static class User {
private String name;
private Integer age;
}
}
2.1.2 创建实体类 UserConfiguration
@Data
public class UserConfiguration {
private UserProperties userProperties;
public UserConfiguration(UserProperties userProperties) {
this.userProperties = userProperties;
}
}
2.1.3 创建配置类 TestAutoConfiguration
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(UserProperties.class)
public class TestAutoConfiguration {
@Bean
public UserConfiguration userConfiguration(UserProperties properties) {
return new UserConfiguration(properties);
}
}
2.1.4 创建文件 META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ys.starter.config.TestAutoConfiguration
2.1.5 pom 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ys</groupId>
<artifactId>test-spring-boot-starter</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.6.13</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.1.6 工程文件结构
2.2 打包部署
2.2.1 执行 mvn clean install,将项目打成 jar 包,部署到本地 maven 仓库
2.2.2 新建一个 Maven 工程,pom 文件添加相关依赖
<dependency>
<groupId>com.ys</groupId>
<artifactId>test-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>
2.3 现象演示
2.3.1 启动项目,运行 main 方法
@SpringBootApplication
public class BlogApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);
Object bean = context.getBean(TestAutoConfiguration.class);
System.out.println(bean);
}
}
通过日志,得出结论:自定义 starter 的自动配置生效
2.3.2 覆盖默认属性
在 test-spring-boot-starter 中 @EnableConfigurationProperties + @ConfigurationProperties 注解的组合使用,并且添加依赖 spring-boot-configuration-processor,我们可以在 idea 中根据提示覆盖默认属性,这也是 SpringBoot 一些自动配置类的常用套路,相关应用可以查看 WebMvcAutoConfiguration 的内部类 WebMvcAutoConfigurationAdapter
2.3.2.1 演示覆盖默认属性值
application.yaml 配置
com:
ys:
prop:
version: 2.0
user:
name: anna
age: 18
启动项目,运行 main 方法
@SpringBootApplication
public class BlogApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);
Object bean = context.getBean(UserConfiguration.class);
System.out.println(bean);
}
}
通过日志,得出结论:默认属性被覆盖
2.3.3 自动配置类一些特征
自动配置类一般都是以 AutoConfiguration 结尾,我们可以通过搜索 AutoConfiguration 来锁定相关类
2.4 扩展:解析以 .json 为后缀的配置文件
starter 可以扩展很多东西,我们不要陷入误区,认为 starter 等价于自动配置。下文将对自定义的 starter 进行扩展,让它可以解析以 .json 为后缀的配置文件
2.4.1 配置文件前置知识点
2.4.1.1 默认配置文件位置
- optional:classpath:/
- optional:classpath:/config/
- optional:file:./
- optional:file:./config/
- optional:file:./config/*/
2.4.1.2 默认配置文件前缀
- application
2.4.1.3 默认配置文件后缀
- yml
- yaml
- properties
- xml
2.4.1.4 默认配置文件
所以默认情况下,以下文件(文件在指定位置)都可以认为是配置文件:
- application.yml
- application.yaml
- application.properties
- application.xml
更多配置相关知识,可以移步相关博文:SpringBoot之外部化配置
2.4.2 让 SpringBoot 将 application.json 也当成配置文件解析
2.4.2.1 相关代码准备
2.4.2.1.1 创建工具类 JsonToPropertySourceConverter
public class JsonToPropertySourceConverter {
public static PropertySource<?> convertJsonToPropertySource(String name, String json) {
try {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map = objectMapper.readValue(json, Map.class);
return new MapPropertySource(name, map);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
2.4.2.1.2 创建Loader JsonSourceLoader
public class JsonSourceLoader implements PropertySourceLoader {
@Override
public String[] getFileExtensions() {
return new String[]{"json"};
}
@Override
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
List<PropertySource<?>> result = new ArrayList<>();
if (resource == null || !resource.exists()) {
return result;
}
ByteArrayOutputStream bao = new ByteArrayOutputStream();
try (InputStream in = resource.getInputStream()) {
IOUtils.copy(in, bao);
}
String json = new String(bao.toByteArray(), StandardCharsets.UTF_8);
PropertySource<?> propertySource = JsonToPropertySourceConverter.convertJsonToPropertySource(resource.getFilename(), json);
result.add(propertySource);
return result;
}
}
2.4.2.1.3 在 spring.factories 中添加 key 为 PropertySourceLoader 的配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ys.starter.config.TestAutoConfiguration
org.springframework.boot.env.PropertySourceLoader=\
com.ys.starter.loader.JsonSourceLoader
2.4.2.1.4 重新打包部署 test-spring-boot-starter
执行命令 mvn clean install
2.4.2.2 现象演示
2.4.2.2.1 创建文件 application.json
在依赖 test-spring-boot-starter.jar 的项目里创建文件 application.json,文件明细如下:
{
"my.starter.key": "666"
}
2.4.2.2.2 启动项目,运行 main 方法
@SpringBootApplication
public class BlogApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);
String property = context.getEnvironment().getProperty("my.starter.key");
System.out.println(property);
}
}
通过日志,得出结论:成功获取到属性 my.starter.key 的值,即 application.json 被当成配置文件解析