【发布时间】:2019-02-22 11:47:22
【问题描述】:
我正在尝试根据当前活动配置文件加载外部属性文件
以及我定义的属性文件如下:
default -> resources/config/application.properties (for dev)
qa -> c:\external-configuration\config\application-qa.properties
prod -> c:\external-configuration\config\application-prod.properties
如何将 spring 配置为从不同来源读取所有这些application*.properties?
我尝试如下定义PropertySourcesPlaceholderConfigurer,但spring可以根据活动配置文件解析属性值,我总是得到application.properties中定义的默认值
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource("c:\external-configuration\config\application-qa.properties"),new FileSystemResource("c:\external-configuration\config\application-prod.properties"));
properties.setIgnoreResourceNotFound(false);
return properties;
}
【问题讨论】:
-
so所有的配置都有不同的key-> value?如果是,为什么不合并到一个属性文件中,如果他们有相同的key,你应该自己做
-
你能在'application.properties'中设置'spring.profiles.active=dev, qa, etc',还是在运行jar时设置命令行参数t加载特定属性'java -jar -Dspring .profiles.active=生产演示-0.0.1-SNAPSHOT.jar'
-
是的,当我使用 -Dspring.profiles.active=qa 选项运行 jar 文件时,我仍然得到在 application.properties 中定义的值,而不是在 application-qa.properties 中定义的值
标签: spring spring-boot properties-file