2015-10-17 22 views
1

我加載如下使用PropertyPlaceholderConfigurer性質在我的春節WebApplication文件:替代屬性在Spring Web應用程序文件在運行時

<bean 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath:db.properties</value> 
       <value>classpath:mail.properties</value> 
      </list> 
     </property> 
    </bean> 

現在,我想從mail.properties覆蓋的一些屬性,所以我創建了一個額外的在我application-context文件閱讀本post項,如下所示:

<context:property-placeholder location="file:override.properties" 
     order="-1" ignore-unresolvable="true" ignore-resource-not-found="true" /> 

然後,在我的Tomcat Server,在啓動配置的VM Arguments,我提供的附加條目:

-Dexternal.props="/Users/ArpitAggarwal/Desktop/override.properties"

隨着一些我必須重寫鍵的覆蓋值。

但是,WebApp是而不是的值從override.properties。任何人都可以幫助我搞清楚,我錯過了什麼。

任何幫助將不勝感激。謝謝!

回答

1

我做了什麼解決該問題是,取而代之的是location屬性作爲${ext.properties.dir:classpath:}/override.properties,如下所示:

<context:property-placeholder 
     location="${ext.properties.dir:classpath:}/override.properties" order="-1" 
     ignore-unresolvable="true" ignore-resource-not-found="true" /> 

並供給ext.properties.dir值從application-server/jvm爲:

-Dext.properties.dir=file:/Users/ArpitAggarwal/properties/ 

參考6-tips-for-managing-property-files-with-spring

0

我認爲使用多個propertyPlaceholders不是一個好主意。 當兩個xml配置在同一個上下文中加載每個屬性並嘗試交叉使用時,我遇到了很多問題。

如果要外部化屬性文件,我會建議一個JNDI屬性:

<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath:db.properties</value> 
      <value>classpath:mail.properties</value> 
      <value>${java:com/env/myApp/externalProperties} 
     </list> 
    </property> 
</bean> 

如果這JNDI的價值將是:「文件:/路徑到propertiesFile」。

這樣你只需要一個propertyePlaceholder。 另請注意,通過將外部文件作爲最後一個位置,如果您有重複的鍵,spring將僅保留最後一個。

0

你只是在你的應用程序上下文中使用

<context:property-placeholder location="file:///${external.props}" 
    order="-1" ignore-unresolvable="true" ignore-resource-not-found="true" /> 

問題是您剛纔使用不當的位置,實際位置是VM ARG可變密鑰=> $ {} external.props

+0

這使得必須從服務器提供'override.properties'作爲'vm arg'。 – Arpit

相關問題