一、引入Maven坐标
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
二、定义自定义属性
/** * @author zhangboqing * @date 2019-11-20 */ @Component @ConfigurationProperties(prefix = "my.custom.property") public class MyCustomProperties { private String name; private String username; private String age; @DeprecatedConfigurationProperty(reason = "换名称了",replacement = "username") public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
三、重新 compile 编译项目之后,configuration processor 会为我们创建一个 JSON 文件:spring-configuration-metadata.json
四、然后当我们在 application.properties 和 application.yml 中写配置的时候就会有自动提醒了
注意:只有先编译后生成spring-configuration-metadata.json文件后,提示功能才生效