java - Spring Boot 类路径配置文件覆盖外部 application.properties
问题描述
我想让 Spring 加载一个外部配置文件(如果存在),否则使用 src/main/resources 中提供的一次。
当前设置:
src/main/resources/application.properties
src/main/resources/application-dev.properties
src/main/resources/application-prod.properties
/this/is/an/external/dir/application-dev.properties
与https://stackoverflow.com/a/27776123/5126654类似,我添加了以下注释:
@EnableFeignClients
@SpringBootApplication
@EnableEncryptableProperties
@PropertySources({ //
@PropertySource("classpath:application.properties"), //
@PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true) //
})
public class Application extends SpringBootServletInitializer {
// ....
}
并在我的 application.properties 中有以下条目:
external.config=/this/is/an/external/dir/application-dev.properties
我还可以看到外部配置文件被拾取。我遇到的问题是类路径属性文件中的每个条目都会覆盖外部条目。即,不是从 /this/is/an/external/dir/application-dev.properties 获取条目,而是从 src/main/resources/application-dev.properties 获取条目。
如何修改我的代码以使外部文件覆盖类路径文件的条目?
解决方案
由于您只需要用于开发的外部配置,因此您也可以考虑在项目的 IDE 运行配置中将外部属性源设置为命令行参数。
--spring.config.location=/this/is/an/external/dir/application-dev.properties
以上述方式指定的属性源会覆盖类路径中存在的 application.properties。
推荐阅读
- javascript - React:如何生成一个 pdf,其中一个组件乘以不同的道具三倍
- regex - 需要正则表达式选择html标签的负模式选择
- perl - Perl PDL:如何更改矩阵中的值
- c# - 如何在 xamarin 表单中指定触摸的位置(X,Y)?
- python - 使用 keras 将新输入输入到中间层时出现错误
- spring-boot - 春季卡夫卡消费者有时不起作用
- python - 当我在上一个 if 语句中打开子进程时,有没有办法关闭子进程
- javascript - 意外的令牌 { 当我需要“express-fileupload”时
- python - 使用文件夹结构在 Keras 中进行预测时如何获得正确的标签?
- php - 使用键:值对从 JSON 中过滤项目