Effect
When using development tools such as Idea, the corresponding completion prompt will appear when entering a prefix in the configuration file, allowing developers to easily configure the corresponding properties. The screenshot of the effect is as follows:
Metadata description
These hints come from the definition of content in the source data files spring-configuration-metadata.json
and in the spring automatic configuration specification additional-spring-configuration-metadata.json
. With fixed specifications in place, development tools can load corresponding information completion prompts from the corresponding json source data files, making the development process more friendly and convenient.
spring-configuration-metadata.json
spring-boot-configuration-processor
Automatically generated by plug-ins , additional-spring-configuration-metadata.json
additional configurations require manual maintenance. This additional configuration file is created because automatic generation will be overwritten after each compilation. Automatic generation needs to be combined with the defined Java class and modified with @ConfigurationProperties
annotations before it can be spring-boot-configuration-processor
loaded by the plug-in and automatically generated into it spring-configuration-metadata.json
.
Attachment: Several other main files about SpringBoot automatic configuration
Configuration file:spring.factories
Configuration file:spring-configuration-metadata.json
andadditional-spring-configuration-metadata.json
Configuration file:spring-autoconfigure-metadata.properties
Plug-in:spring-boot-configuration-processor
Plug-in:spring-boot-autoconfigure-processor
DeferredImportSelector selector:AutoConfigurationImportSelector
Development and implementation
In order to achieve the above effect, the following is a specific code example:
1. Define a Java configuration class
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 测试配置类
*
* @author shanhy
*/
@Data
@Component
@ConfigurationProperties(prefix = "shanhy.test")
public class TestProperties {
/**
* 唯一ID
*/
private int id = 100;
/**
* 名称
*/
private String name;
/**
* 标志位
*/
private boolean flag = true;
}
The Java annotations above the attributes will eventually be automatically generated into the metadata file spring-configuration-metadata.json
, so please add the annotation description strictly.
2. Add plug-in dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
This plug-in automatically generates the content of the metadata file. If you do not need to use the plug-in to automatically generate metadata, but want to configure the metadata of your own defined attributes, you can maintain it yourself
resources/META-INF/additional-spring-configuration-metadata.json
.
3. Compile and verify the results
Use Mavan to compile and generate metadata files, and then verify the results in properties.
To verify the custom appended metadata, I resources/META-INF/additional-spring-configuration-metadata.json
added a custom configuration in the file:
{
"properties": [
{
"name": "shanhy.test.additional-content",
"type": "java.lang.Boolean",
"description": "附加内容"
}
]
}
(END)