2013-10-08 54 views
0

我的其餘客戶端將系統屬性service.mode設置爲prod或uat或dev。根據此屬性,應加載一個文件dev.properties,prod.properties,uat.properties的相應屬性。這個屬性將被不同的服務bean定義的spring的applicationContext.xml使用。根據系統屬性模式(Dev/Prod/UAT)加載適當的屬性文件

根據系統屬性service.mode加載相應屬性的最佳方法是什麼?

回答

0

最好的方法是在你的spring配置文件中使用PropertyPlaceholderConfigurer。像這樣

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location"> 
      <value>file:${service.mode}</value> 
     </property> 
</bean> 

然後你就可以訪問你的bean中這樣的性質

<bean id="initSystemStatus" class="xxxx.xxxx.xxxx.InitSystemStatus"> 
     <constructor-arg index="0" value="${application.context.instanceID:0001}" /> 
     <constructor-arg index="1" value="${application.context.timeout:2000}"/> 
</bean> 

注:

${application.context.instanceID:0001}0001是默認值,如果屬性application.context.instanceID沒有提供在適當的文件中。

0

使用PropertyPlaceholderConfigurer如下

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location"> 
      <value>${service.mode}.properties</value> 
     </property> 
</bean> 

現在,當您啓動其他客戶端,確保傳遞從提示service.mode
(如-Dservice.mode=uat

編輯

如果不提供service.mode然後使用以下配置

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location"> 
      <value>${service.mode:prod}.properties</value> 
     </property> 
</bean> 

請注意提及財產的方式${service.mode:prod}。這意味着如果service.mode無法解析,那麼默認值即prod將被拾取。

注:這僅適用於SPRING 3.X

+0

什麼已經給寫了相同的答案了吧? – TheEwook

+0

「PropertyPlaceholderConfigurer」的配置是相同的,但如果你仔細舉例說明OP的問題,他需要一個標誌(dev/uat等),讓應用程序選擇屬性文件。用戶只會傳遞dev/uat,而不會傳遞你所假設的dev.properties/uat.properties。 – Santosh

+0

沒有提供service.mode是否可以通過'PropertyPlaceholderConfigure'來選擇默認的prod.poroperties文件。 – dumper