我正在尝试在 application.properties 文件中添加自定义变量。当我添加时,IDE(Eclipse 2019-06 (4.12.0) 和 Spring Tool Suite 4.3.1)显示黄色警告“abc.def 是未知属性”。
问题类似于Spring boot - custom variables in Application.properties . 我在答案中尝试了@StephaneNicoll 给出的方法。它仍然显示相同的警告。其实我打开项目https://github.com/snicoll-demos/spring-boot-configuration-binding在 Eclipse 中。它仍然给出相同的警告。还有什么我需要做的吗?
这是一个小麻烦。但是想摆脱它。
最佳答案
您必须将 spring-boot-configuration-processor
添加到您的项目中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
然后使用注释 @ConfigurationProperties
及其前缀创建您的属性类,例如:
@Data
@ConfigurationProperties(prefix = "abc")
@Validated
public class AbcProperties {
/**
* Description of the property 'def'.
*/
@NotNull private Long def = 0L; // <- default value of the property
// other props...
}
然后启用它:
@SpringBootApplication
@EnableConfigurationProperties(AbcProperties.class)
public class AbcApplication {
//...
}
然后重建您的项目。完成这些步骤后,IDE 将正确看到您的 Prop 。
更多信息:
关于Spring boot - Application.properties 中的用户定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57116707/