[英]Spring boot classpath configuration files overrides external application.properties
I want to have Spring load an external configuration file if present and else use the once provided in src/main/resources.我想让 Spring 加载一个外部配置文件(如果存在),否则使用 src/main/resources 中提供的一次。
Current setup:当前设置:
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
Similar to https://stackoverflow.com/a/27776123/5126654 I added the following annotations:与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 {
// ....
}
And have the following entry in my application.properties:并在我的 application.properties 中有以下条目:
external.config=/this/is/an/external/dir/application-dev.properties
I also can see that the external conifguration file is picked up.我还可以看到外部配置文件被拾取。 The problem I have is that every entry in the classpath properties file overwrites the external ones.我遇到的问题是类路径属性文件中的每个条目都会覆盖外部条目。 Ie instead of taking entries from /this/is/an/external/dir/application-dev.properties entries from src/main/resources/application-dev.properties are taken.即,不是从 /this/is/an/external/dir/application-dev.properties 获取条目,而是从 src/main/resources/application-dev.properties 获取条目。
How can I modify my code such that the external file overrides the entries of the classpath file?如何修改我的代码以使外部文件覆盖类路径文件的条目?
Just change the order of importing.只需更改导入顺序即可。 Something like:就像是:
<context:property-placeholder file-encoding="UTF-8"
location="${YOUR_CUSTOM_PATH}/global.properties ,
${YOUR_CUSTOM_PATH}/local.properties" ignore-unresolvable="true"/>
or或者
@PropertySource(value = {"classpath:global.properties" , "local.properties"},ignoreResourceNotFound = true)
In this case the local.properties
overrides properties of global.properties
在这种情况下, local.properties
会覆盖global.properties
的属性
Since you only want the external config for development, you could as well consider setting external property source as a command line argument in the IDE run configuration for your project.由于您只需要用于开发的外部配置,您还可以考虑在项目的 IDE 运行配置中将外部属性源设置为命令行参数。
--spring.config.location=/this/is/an/external/dir/application-dev.properties
The property source specified in the above manner overrides the application.properties present in class path.以上述方式指定的属性源会覆盖 class 路径中的 application.properties。
I want to have Spring load an external configuration file if present and else use the once provided in src/main/resources.我想让 Spring 加载一个外部配置文件(如果存在),否则使用 src/main/resources 中提供的一次。
That's the default behavior of Spring Boot.这是 Spring 引导的默认行为。 So you just need to enable it correctly.所以你只需要正确启用它。
First of all, remove next annotations, they may disrupt/override the default mechanism:首先,删除下一个注释,它们可能会破坏/覆盖默认机制:
@PropertySources({ //
@PropertySource("classpath:application.properties"), //
@PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true) //
})
Then specify location of your file as a command line argument:然后将文件的位置指定为命令行参数:
java -jar myapp.jar --spring.config.additional-location=/this/is/an/external/dir/
More information here:更多信息在这里:
Then either rename your config to application.properties
or specify your dev
profile (suffix in your file name) via another environmental variable:然后将您的配置重命名为application.properties
或通过另一个环境变量指定您的dev
配置文件(文件名中的后缀):
-Dspring-boot.run.profiles=dev
Related question: Setting active profile and config location from command line in spring boot相关问题: 在 spring 引导中从命令行设置活动配置文件和配置位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.