2014-04-01 36 views
2

我想配置Spring覆蓋生產期間的一些屬性,但只有(!)如果production.properties文件被找到,並且只有它定義的屬性。但是這些限制只適用於這個生產檔案。所有其他屬性文件應該是必需的。如何在Spring配置中導入用於生產的可選屬性文件?

但我無法在彈簧配置中導入兩個不同的屬性資源。我需要改變什麼?

@Configuration 
@PropertySource({"classpath:default.properties"}) 
@PropertySource({"file:production.properties"}, ignoreResourceNotFound = true) //Error: Duplicate annotation 

回答

2

嘗試PropertySources註釋,這是一個容器註釋,聚集若干PropertySource註解。

0

你可以在配置文件中的以下內容:

@Bean 
public static PropertySourcesPlaceholderConfigurer properties() { 
    final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); 

    Resource[] resources = new Resource[ ] { 
      new ClassPathResource("default.properties"), 
      new FileSystemResource("production.properties") 
    }; 

    pspc.setLocations(resources); 
    pspc.setIgnoreResourceNotFound(true); 
    pspc.setIgnoreUnresolvablePlaceholders(false); 
    return pspc; 
} 
+0

這是不是設置'ignoreSourceNotFound'屬性我所有的財產文件?我想只適用於一個特定的。 – membersound

+0

ignoreSourceNotFound在這種情況下意味着如果Spring遇到類似@Value(「$ {prop.name})的東西,並且在任何屬性文件中找不到prop.name,那麼應用程序就不會啓動。所有屬性都需要存在於所有文件中 – geoand

相關問題