spring - Spring boot - Application.properties 中的用户定义变量
问题描述
我正在尝试在 application.properties 文件中添加自定义变量。当我添加时,IDE(Eclipse 2019-06 (4.12.0) 和 Spring Tool Suite 4.3.1)显示黄色警告“abc.def 是未知属性”。
该问题类似于Application.properties 中的 Spring boot-custom variables。我尝试了@StephaneNicoll 在答案中给出的方法。它仍然显示相同的警告。其实我在Eclipse中打开了项目https://github.com/snicoll-demos/spring-boot-configuration-binding 。它仍然给出相同的警告。还有什么我需要做的吗?
这是一个小烦恼。但想摆脱它。
解决方案
您必须添加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 将正确地看到您的道具。
更多信息:
推荐阅读
- sql - 在 Where 子句中使用 Case 语句 ___ 将日期范围与 BETWEEN 一起使用
- flutter - Flutter:设置 AnimatedList 动画时长
- node.js - botbuilder-storage-postgres 是否需要中间件?
- batch-file - 创建一个批处理文件来切换控制面板设置 - “当我合上盖子时:”
- sql - 从长字符串的中间提取字符串 - SQL
- ruby - ruby 中的转储方法是否类似于 perl 的 Data::Dumper->Dump(object) 方法?
- node.js - 我无法发送电子邮件 - Firebase CloudFunctions
- mysql - MySQL - 使用文本列更新所有表
- python-3.x - 试图从 newegg.com 获取价格,不断收到索引错误
- java - ExecutorService InvokeAny 正在运行所有可调用对象以完成