问题描述:
SpringBoot通过@ConfigurationProperties增加配置后,在yml中对属性进行配置时无提示,不方便属性配置
原因分析:
生成的jar文件中没有spring-configuration-metadata.json,可能的原因:
- 未配置@EnableConfigurationProperties
- 未配置spring-boot-configuration-processor依赖
- 如果是使用idea,未配置使能注解处理器
解决方案:
- 未配置@EnableConfigurationProperties,需要在相应的配置类上增加注解,指定对应的配置类
@EnableConfigurationProperties(XxxProperties.class)
public class XxxAutoConfig {
private XxxPropertiesproperties;
}
另外还有一种情况:如果之前是通过@Component使@ConfigurationProperties生效的,应去掉@Component改用@EnableConfigurationProperties
2. 未配置spring-boot-configuration-processor依赖问题,需要在项目中引入spring-boot-configuration-processor依赖,如下:
<!--配置注解处理 @ConfigurationProperties annotation processing (metadata for IDEs) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 如果是使用idea需要配置使能注解处理器,确保Enable annotation processing勾选
总结:
要想使用@ConfigurationProperties注解的配置属性在yml中输入时,idea有相应的提示,需要:
- 需要使用@EnableConfigurationProperties注解使其生效
- 配置spring-boot-configuration-processor依赖
- 使能idea注解处理器
最终在jar中生成spring-configuration-metadata.json
参考:
https://blog.youkuaiyun.com/yuhan_0590/article/details/85100246