咱們在引用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