spring-boot - 加载活动属性文件 spring boot
问题描述
我想从活动配置文件中读取值,或者可以说活动属性文件。我有三个属性文件
- 应用程序-dev.properties
- 应用程序-stage.properties
- 应用程序-prod.properties
我已将活动配置文件设置dev
为如下
spring.profiles.active=dev
我的application-dev.properties
文件有一个我想在课堂上阅读的条目。
应用程序-dev.properties 文件
fix.connection.type=initiator
我尝试阅读此条目
@Configuration
@PropertySource("classpath:application-${spring.profiles.active}.properties")
@Component
public class AdaptorDestination {
@Value("${fix.connection.type}")
private String connectionType;
}
例外
nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in value "classpath:application-${spring.profiles.active}.properties"
请帮帮我
解决方案
您无需使用@PropertySource
注释即可使用注释加载属性@Value
。Spring 将@Value
自动从当前活动配置文件加载属性。
此外,您不需要${spring.profiles.active}
在@PropertySource
注释中指定,因为默认情况下 Spring 总是从当前活动的配置文件中加载属性。Spring 根据配置文件后缀自动解析文件名。
所以您只需要指定基本文件名,例如:
@PropertySource("classpath:application.properties")
如果当前活动的配置文件是“dev”,Spring 将首先从application.properties
文件加载属性,然后用文件中的属性覆盖它们application-dev.properties
。
你可以在这里阅读更多
推荐阅读
- java - PDFBox 识别多列
- php - 让旧的 TYPO3 页面在本地运行
- asp.net - 如何登录我的 Angular 前端应用程序之一,然后导航到其他前端应用程序而无需再次要求身份验证
- c# - Directory.GetFiles 搜索模式返回无效结果
- python - Speech_recognition 模块 python 在 Windows 上更好
- python - 字符串匹配返回不匹配的默认值
- python - Pandas:根据目标分布从 DataFrame 中采样
- php - 如何将数据从控制器保存到一个地方。那么,我可以将它用于 Laravel 中的所有控制器或视图吗?
- python-3.x - 如何计算Python数据框中段落中每个句子中特定单词的出现次数
- c++ - c++ libcurl和SSL设置https读取文件