咱们在引用spring官方start库或者第三方start库时,在写配置文件时idea老是能精准的提示,而且鼠标能够点过去看具体属性或者类,而本身写的配置文件idea只会有“Cannot resolve configuration property ...”这样的提示。html
咱们如今也去配置咱们本身的配置文件让idea知道这些配置文件是干什么的。java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
复制代码
@Data
public class School {
private Integer no;
private String name;
private String address;
}
复制代码
@Data
@ConfigurationProperties(prefix = "jiuxian")
public class JiuxianProperties {
private String name;
private String nameCn;
private String nameEn;
private String[] hobbies;
private SexEnum sexEnum;
private boolean single;
private School school;
private City city;
enum SexEnum {
MAN, WOMAN
}
@Data
static class City {
private String no;
private String name;
}
}
复制代码
这个时候@ConfigurationProperties(prefix = "jiuxian") 注解会报错not registered via @EnableConfigurationProperties or marked as Spring component。这须要加上@EnableConfigurationProperties就能够了git
@EnableConfigurationProperties({ JiuxianProperties.class })
@SpringBootApplication
public class SpringbootYmlApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootYmlApplication.class, args);
}
}
复制代码
而后输入 mvn clean packagegithub
这个时候properties和yml文件已经能够提示了,不过有一些会有问题,好比jiuxian.school,这是一个对象,idea并不能解析,而由于City对象是个内部类是能够解析的。spring
如图:json
如何处理呢? 只须要加上 @NestedConfigurationProperty注解便可springboot
@NestedConfigurationProperty
private School school;
复制代码
而后输入命令 mvn clean packageide
警告消失(会有一点延迟)spring-boot
为何必定要打包编译后才能够呢?idea
看一下打包事后的结构,如图:
idea之因此会代码提示就是由于这个spring-configuration-metadata.json json文件。这个文件的生成依据的就是咱们上面的配置。
若是想在配置文件中可以提示该字段描述,以及该字段可选参数,是否弃用等信息时,须要加额外的配置
(1)该配置文件的节点信息
"groups"是高级别的节点,它们自己不指定一个值,但为properties提供一个有上下文关联的分组。例如,server.port和server.servlet-path属性是server组的一部分。
注:不须要每一个"property"都有一个"group",一些属性能够以本身的形式存在。
properties
hints 能够给属性提供可选的值,以级描述
详细看示例
{
"groups": [
{
"name": "jiuxian",
"sourceType": "com.jiuxian.config.JiuxianProperties",
"type": "com.jiuxian.config.JiuxianProperties"
},
{
"name": "jiuxian.school",
"sourceType": "com.jiuxian.config.School",
"type": "com.jiuxian.config.School",
"sourceMethod": "getSchool()"
}
],
"properties": [
{
"name": "jiuxian.name",
"sourceType": "com.jiuxian.config.JiuxianProperties",
"type": "java.lang.String",
"deprecation": {
"level": "error",
"reason": "replacement nameCn.",
"replacement": "jiuxian.name-cn"
}
},
{
"name": "jiuxian.name-cn",
"sourceType": "com.jiuxian.config.JiuxianProperties",
"type": "java.lang.String",
"defaultValue": "jiuxian"
}
],
"hints": [
{
"name": "jiuxian.sex-enum",
"values": [
{
"value": "man",
"description": "man."
},
{
"value": "woman",
"description": "woman."
}
]
},
{
"name": "jiuxian.single",
"values": [
{
"value": true,
"description": "yes."
},
{
"value": false,
"description": "no."
}
]
}
]
}
复制代码
idea 提示:
(2) 如何废弃某个字段,能够用代码来注解
@Deprecated
private String name;
@Deprecated
public String getName() {
return getNameCn();
}
@DeprecatedConfigurationProperty(replacement = "jiuxian.name-cn", reason = "replacement nameCn")
public void setName(String name) {
setNameCn(name);
}
复制代码
【注】以上代码基于Springboot 2.0