動態設置spring配置PropertyPlaceholderConfigurer location的路徑


  在spring中經常將常用的屬性放在properties文件中,然后再spring的配置文件中使用PropertyPlaceholderConfigurer引用properties文件。
對於web項目來說,可以通過相對路徑得到配置文件的路徑,而對於可執行項目,在團隊開發中就需要根據各自的環境來指定properties配置文件的路徑了。
對於這種情況可以將配置文件的路徑放在java虛擬機JVM的自定義變量中,例如:-Dconfig.path=/yourpath/1.properties
如下在配置文件中使用$config.path來引用JVM自定義變量的值就可以了:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN""http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans>        
<bean id="configBean"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">           
  <property name="location">               
    <value>$config.path</value>           
  </property>       
</bean> 
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">           
  <property name="helloWord">               
    <value>${onlyfun.caterpillar.helloWord}</value>           
  </property>       
</bean>
</beans>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM