我正在處理一個Spring應用程序,我意識到我有一個與我管理我的屬性的方式有關的問題。 我使用Spring環境配置文件爲了加載我的屬性,我最近添加了更多的配置文件,使我的屬性文件不可管理。正確使用Spring環境配置文件爲了管理PropertySourcesPlaceholderConfigurer和屬性文件集
屬性文件位於src/main/resources/META-INF/props/
內的不同文件夾中,其中eah文件夾與不同的Spring環境配置文件匹配。
我現在至少有5個配置文件,這意味着我有5個子文件夾,每個文件夾都包含具有相同名稱的屬性文件,但只有一些鍵的值不同。
下面是它的外觀:
這裏是我是如何配置我的PropertyPlaceholders:
@Configuration
public class PropertyPlaceholderConfiguration {
@Profile(Profiles.CLOUD)
static class cloudConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
propertySourcesPlaceholderConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/props/cloud/*.properties"));
return propertySourcesPlaceholderConfigurer;
}
}
@Profile(Profiles.DEFAULT)
static class defaultConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
propertySourcesPlaceholderConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/props/default/*.properties"));
return propertySourcesPlaceholderConfigurer;
}
}
@Profile(Profiles.TEST)
static class testConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
propertySourcesPlaceholderConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/props/test/*.properties"));
return propertySourcesPlaceholderConfigurer;
}
}
@Profile(Profiles.DEV)
static class devConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
propertySourcesPlaceholderConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/props/dev/*.properties"));
return propertySourcesPlaceholderConfigurer;
}
...
}
綜上所述,我的問題是:
- 鍵/值對全部複製到5個不同的文件夾中,因爲只有幾個val你是不同的。
因此,我正在尋找一種新的策略來管理不同環境之間的差異。
任何人都可以請幫忙嗎?
謝謝艾倫!嗯,唯一的是你的解決方案迫使一個在同一個文件中有不相關的屬性(即common.profile.properties)... – balteo
建議的解決方案的另一個問題是,如果由於某種原因某些時候我需要不同的值對於我需要將其從公共文件中提取出來的屬性之一放入特定於配置文件的文件中。 – balteo