在SpringBoot的项目中,往往需要一些配置,使用idea添加配置有些都会给提示,这个是怎么实现的呢。如下图
自定义配置怎么出现提示呢?
使用spring-boot-configuration-processor实现,其作用是生产配置元数据。
1. 添加下列依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2. 在resources添加/META-INF/spring-configuration-metadata.json,也可使用additional-spring-configuration-metadata.json
json文件如下格式:
{
"groups": [],
"hints": [],
"properties": [
{
"name": "muser.name",
"type": "java.lang.String",
"description": "username"
},
{
"name": "muser.password",
"type": "java.lang.String",
"description": "password"
},
{
"name": "muser.host",
"type": "java.lang.String",
"defaultValue": "localhost",
"description": "hostname"
},
{
"name": "muser.port",
"type": "java.lang.Integer",
"defaultValue": "8080",
"description": "port"
}
]
}
3. 对应的Properties:
@ConfigurationProperties(prefix = "muser")
@Component
public class UserProperties {
private String name;
private String password;
private String ip;
private int port;
//getter setter...
}
4. 出现的效果如下:
注:additional-spring-configuration-metadata.json中的默认值不代表Properties中已经设置,spring-boot-configuration-processor仅仅只是辅助作用
更多推荐
SpringBoot之spring-boot-configuration-processor
发布评论