2014-03-24 39 views
0

我們在我們的Spring上下文這個現有的財產裝載機配置方面:財產佔位如何引用屬性豆

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <util:list> 
      <value>hedging-service.properties</value> 
     </util:list> 
    </property> 
</bean> 

<!--Hedging properties bean that can be injected into other beans--> 
<util:properties id="hedgingProperties" location="classpath:hedging-service.properties"/> 

而且是引用hedgingProperties

<bean id="mailProcessor" 
    class="a.b.c.MailProcessor"> 
    <property name="properties" ref="hedgingProperties"/> 
    ... 
</bean> 

我一個bean重構上下文以從多個屬性文件加載並避免重複定義屬性。我第一次嘗試是在地方兩個使用這個bean上述

<context:property-placeholder location="classpath:hedging-service-core.properties,classpath:hedging-service.properties,classpath:icon.properties"/> 

,但我怎麼能留住別名或參考hedgingProperties豆當我使用context:property-placeholder

+0

你的意思是這個'特性-REF = 「hedgingProperties」'? ''的一個屬性。 –

回答

0

答案是混合context:property-placeholderutil:properties

<util:properties id="hedgingProperties" location="classpath:hedging-service.properties"/> 
<context:property-placeholder properties-ref="hedgingProperties" /> 

<bean id="mailProcessor" class="a.b.c.MailProcessor"> 
    <property name="properties" ref="hedgingProperties"/> 
    ... 
</bean> 
+0

是和否util:屬性不支持通過位置值加載多個文件 – emeraldjava

+1

那麼,使用'PropertiesFactoryBean'而不是'locations'屬性 –

+0

@ArtemBilan我同意。對於多個屬性文件看起來像合理的解決方案 –