2015-05-20 60 views
2

我在我的應用程序兩個模塊的佔位符:

  • 核心
  • 網絡

core模塊包含以下特性spring/applicationContext-core.xml上下文中的佔位符配置:

<bean id="coreProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="locations"> 
     <list> 
      <value>classpath:/properties/*.properties</value> 
      <value>classpath:/profiles/${build.profile.id}/properties/*.properties</value> 
      <value>file:${ui.home}/profiles/${build.profile.id}/properties/*.properties</value> 
     </list> 
    </property> 
</bean> 

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> 
    <property name="ignoreResourceNotFound" value="false"/> 

    <property name="properties" ref="coreProperties" /> 
</bean> 

<bean id="propertySourcesPlaceholderConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/> 
</bean> 

考慮到我有以下特性:

resource.suffix=.min 

如果我注入的core@Component這個值:

@Value("${resource.suffix}") 
private String resourceSuffix; 

該物業妥善解決。

但是,如果我在一個bean添加web模塊,它只是加載核心配置以及內部相同的配置:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/servlet-context.xml /WEB-INF/application-security.xml 
     classpath*:/spring/applicationContext-core.xml</param-value> 
</context-param> 

則該屬性不解決,resourceSuffix值設置爲以下字符串文字值${resource.suffix

我錯過了什麼?

+0

的@Value( 「$ {} resource.suffix」)又是一個屬性,而不是在constuctur說法對不對? 你也確定Web模塊bean是由spring實例化/掃描的嗎? –

+0

它位於'@ Controller'內的一個字段上,並且該bean被掃描,因爲它也有其他'@ Autowired' bean被注入。 –

+0

哪個上下文實例化「web」模塊? 你可以嘗試將propertySourcesPlaceholderConfigurer移動到該上下文中嗎? –

回答

2

我相信這與彈簧如何處理前/後處理器有關。

基本上你可以有重複的定義或使用不同的機制來加載屬性。

就像我之前所知道的那樣,重複是唯一的方法。

更多關於http://www.baeldung.com/2012/02/06/properties-with-spring/#parent-child

+1

謝謝蟎。現在我必須閱讀Eugen的所有帖子,以免我自己受到一些與Spring配置相關的挫折。 –

+0

我認爲@PropertySource是一個更好的選擇,除非遺留代碼,然後重複是要走的路 –