开源项目SDK:https://github.com/mingyang66/spring-parent
个人文档:https://mingyang66.github.io/raccoon-docs/#/
spring-boot-configuration-processor是springboot提供的一个注释处理器(annotation processor),它用于在编译时生成元数据文件(META-INF/spring-configuration-metadata.json),该文件描述了你的配置属性,以便为开发者提供自动补全和文档支持。
以下是使用spring-boot-configuration-processor的基本步骤:
- 在pom.xml中添加spring-boot-configuration-processor依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
注意true标记,这个标记意味着依赖仅用于编译时,而不会包含在最终的包中。
- 定义属性配置
使用@ConfigurationProperties和相关注解(如:@Value、@ConfigurationProperty)来定义你的配置属性
@ConfigurationProperties(prefix = RedisDbProperties.PREFIX)
public class RedisDbProperties {
/**
* 属性配置前缀
*/
public static final String PREFIX = "spring.emily.redis";
/**
* 是否开启数据源组件, 默认:true
*/
private boolean enabled = true;
/**
* 默认配置标识
*/
private String defaultConfig;
...
}
- 编译项目
当你编译你的项目时,spring-boot-configuration-processor
会自动运行并生成 META-INF/spring-configuration-metadata.json
文件。示例如下:
{
"groups": [
{
"name": "spring.emily.redis",
"type": "com.emily.infrastructure.redis.RedisDbProperties",
"sourceType": "com.emily.infrastructure.redis.RedisDbProperties"
}
],
"properties": [
{
"name": "spring.emily.redis.client-type",
"type": "org.springframework.boot.autoconfigure.data.redis.RedisProperties$ClientType",
"description": "客户端类型",
"sourceType": "com.emily.infrastructure.redis.RedisDbProperties"
},
{
"name": "spring.emily.redis.config",
"type": "java.util.Map<java.lang.String,com.emily.infrastructure.redis.RedisProperties>",
"description": "多数据源配置",
"sourceType": "com.emily.infrastructure.redis.RedisDbProperties"
},
{
"name": "spring.emily.redis.default-config",
"type": "java.lang.String",
"description": "默认配置标识",
"sourceType": "com.emily.infrastructure.redis.RedisDbProperties"
},
{
"name": "spring.emily.redis.enabled",
"type": "java.lang.Boolean",
"description": "是否开启数据源组件, 默认:true",
"sourceType": "com.emily.infrastructure.redis.RedisDbProperties"
}
],
"hints": []
}
- 使用元数据
生成的元数据文件可以被 IDE(如 IntelliJ IDEA 或 Visual Studio Code)的 Spring Boot 插件使用,以提供自动补全和文档支持。