讓spring的配置文件可以使用外部properties里的參數。


property文件global-config-file.properties:

 

Property代碼   收藏代碼
  1. #FOR dataSource  
  2. jdbc.dataSource.url=jdbc:postgresql://192.168.1.118:5432/DB_name  
  3. jdbc.dataSource.username=postgres  
  4. jdbc.dataSource.password=123  

 

示例配置的是數據源參數。

 

之后在springContext的配置文件中,加入下面代碼,:

 

Xml代碼   收藏代碼
  1. <bean id="propertyConfigurer"    
  2.          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  3.        <property name="ignoreUnresolvablePlaceholders" value="true" />  
  4.        <property name="location" value="classpath:global-config-file.properties"/>    
  5.    </bean>   

   即引入PropertyPlaceholderConfigurer來讀取property配置文件,spring框架會從其那里獲取到需要的配置參數。

 

下面這種是引入properties文件不在src目錄下的時候。

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>config/settings.properties</value>  //此文不在src下時,要這樣寫
<value>classpath:c3p0.properties</value>
<value>classpath:mchange-log.properties</value>
<value>classpath:mchange-commons.properties</value>
</list>
</property>
</bean>

 

之后再用${key}的格式取代你數據源配置參數:

 

Xml代碼   收藏代碼
  1. <bean id="myBatisDataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  2.         <property name="driverClassName" value="org.postgresql.Driver">  
  3.         </property>  
  4.         <property name="url"  
  5.             value="${jdbc.dataSource.url}">     
  6.         </property>  
  7.         <property name="username" value="${jdbc.dataSource.username}"></property>  
  8.         <property name="password" value="${jdbc.dataSource.password}"></property>  
  9.     </bean>  

 例如:${jdbc.dataSourcurl}:框架會將global-config-file.properties讀到jdbc.dataSource.url的值“jdbc:postgresql://192.168.1.118:5432/DB_name”填入${jdbc.dataSource.url}所在的位置。

 

 

 

 

 

2,將property配置文件與工程分離(獨立到服務器中)

   為什么想到要這樣做呢?

   這是在實際開發部署中,每次將工程打包部署到服務器,都需要在服務器端修改工程的配置項(如數據源等),感覺很麻煩。

   如果工程優先讀取放置在服務器上的配置文件,沒有此配置文件才從工程目錄里面讀取,那樣就可以將只在服務器存放一份配置文件並修改成與服務器運行環境一致,今后部署即可不用每次都修改工程配置文件了。

2.1 做法:

  剛才我們在第1點添加了一個propertyConfigurer,現將其改名為“propertyConfigurer2”,並添加一個“propertyConfigurer1”,兩者配置為:

 

Xml代碼   收藏代碼
  1. <bean id="propertyConfigurer1"    
  2.           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="order" value="1"/>  
  4.          <property name="ignoreResourceNotFound" value="true"/>  
  5.         <property name="ignoreUnresolvablePlaceholders" value="true" />  
  6.         <property name="location" value="file:C:/tempSys/config/global-config-file.properties"/>    
  7.     </bean>   
  8.   
  9.     <bean id="propertyConfigurer2"    
  10.           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  11.         <property name="order" value="2"/>  
  12.         <property name="ignoreUnresolvablePlaceholders" value="true" />  
  13.         <property name="location" value="classpath:global-config-file.properties"/>    
  14.     </bean>   

 order屬性表示加載順序,這種配置表示先加載propertyConfigurer1,在加載propertyConfigurer2,兩者配置文件中有同樣的參數,后加載的參數不會覆蓋先加載的。

 

  propertyConfigurer1需要添加一個 <property name="ignoreResourceNotFound" value="true"/>屬性,表示當找不到這個配置文件時,則跳過,這樣就不會拋出FileNotFoundException了。

  這樣配置之后,工程會在啟動時,首先檢查C:/tempSys/config/global-config-file.properties是否存在,如果存在則加載器配置,隨后再加載項目目錄下的global-config-file.properties;如果不存在,則直接加載項目目錄下的global-config-file.properties。

   

   這樣做不單可將配置與開發環境分離出來,避免每次部署時繁瑣的配置修改工作。同時這也提高了系統部署時的安全性,比如,實際運行的正式數據庫賬戶密碼信息將是十分機密的信息,由運維人員保管維護,可能對開發人員都不能公開(開發人員只知道開發測試數據庫的信息),將這些信息直接配置在服務器上,服務器也由運維人員進行管理,這樣可以減少機密信息的知曉人群,提高安全性


免責聲明!

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



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