spring配置文件引入properties文件: 標簽使用總結


一、問題描述:

  1、有些參數在某些階段中是常量,比如:

  (1)在開發階段我們連接數據庫時的連接url、username、password、driverClass等

  (2)分布式應用中client端訪問server端所用的server地址、port,service等

  (3)配置文件的位置

  2、而這些參數在不同階段之間又往往需要改變,比如:在項目開發階段和交付階段數據庫的連接信息往往是不同的,分布式應用也是同樣的情況。

二、期望

  能不能有一種解決方案可以方便我們在一個階段內不需要頻繁書寫一個參數的值,而在不同階段間又可以方便的切換參數配置信息

三、解決

  spring3中提供了一種簡便的方式就是<context:property-placeholder>元素

  方法一:只需要在spring的配置文件里添加一句:

<!--引入數據庫配置信息 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

  或者方法二:情況1配置一個

 

   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath*:db/jdbc.properties" />
    </bean>

 

  情況2配置多個

  <bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/opt/demo/config/demo-db.properties</value> 
                <value>classpath:/opt/demo/config/demo-db2.properties</value> 
            </list>
        </property>
    </bean>

  即可,這里location值為參數配置文件的位置,參數配置文件通常放在src目錄下,而參數配置文件的格式跟java通用的參數配置文件相同,即鍵值對的形式,例如:

#jdbc配置
test.jdbc.driverClassName=com.mysql.jdbc.Driver
test.jdbc.url=jdbc:mysql://localhost:3306/test
test.jdbc.username=root
test.jdbc.password=root

  行內#號后面部分為注釋

四、應用

  1.這樣一來就可以為spring配置的bean的屬性設置值了,比如spring有一個jdbc數據源的類DriverManagerDataSource

  在配置文件里這么定義bean:

<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${test.jdbc.driverClassName}"/>
    <property name="url" value="${test.jdbc.url}"/>
    <property name="username" value="${test.jdbc.username}"/>
    <property name="password" value="${test.jdbc.password}"/>
</bean>

  甚至可以將${ }這種形式的變量用在spring提供的注解當中,為注解的屬性提供值

 

===========================================================================

 

外在化應用參數的配置

  在開發企業應用期間,或者在將企業應用部署到生產環境時,應用依賴的很多參數信息往往需要調整,比如LDAP連接、RDBMS JDBC連接信息。對這類信息進行外在化管理顯得格外重要。PropertyPlaceholderConfigurer和PropertyOverrideConfigurer對象,它們正是擔負着外在化配置應用參數的重任。

<context:property-placeholder/>元素

  PropertyPlaceholderConfigurer實現了BeanFactoryPostProcessor接口,它能夠對<bean/>中的屬性值進行外在化管理。開發者可以提供單獨的屬性文件來管理相關屬性。比如,存在如下屬性文件,摘自userinfo.properties。
  db.username=scott
  db.password=tiger

  如下內容摘自propertyplaceholderconfigurer.xml。正常情況下,在userInfo的定義中不會出現${db.username}、${db.password}等類似信息,這里采用PropertyPlaceholderConfigurer管理username和password屬性的取值。DI容器實例化userInfo前,PropertyPlaceholderConfigurer會修改userInfo的元數據信息(<bean/>定義),它會用userinfo.properties中db.username對應的scott值替換${db.username}、db.password對應的tiger值替換${db.password}。最終,DI容器在實例化userInfo時,UserInfo便會得到新的屬性值,而不是${db.username}、${db.password}等類似信息。

<bean id="propertyPlaceholderConfigurer"   
        class="org.springframework.beans.factory.config.  
PropertyPlaceholderConfigurer">  
    <property name="locations">  
        <list>  
            <value>userinfo.properties</value>  
        </list>  
    </property>  
</bean>  
 
<bean name="userInfo" class="test.UserInfo">  
  <property name="username" value="${db.username}"/>  
  <property name="password" value="${db.password}"/>  
</bean> 

  通過運行並分析PropertyPlaceholderConfigurerDemo示例應用,開發者能夠深入理解PropertyPlaceholderConfigurer。為簡化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面給出了配置示例,啟用它后,開發者便不用配置PropertyPlaceholderConfigurer對象了。

<context:property-placeholder location="userinfo.properties"/> 

  PropertyPlaceholderConfigurer內置的功能非常豐富,如果它未找到${xxx}中定義的xxx鍵,它還會去JVM系統屬性(System.getProperty())和環境變量(System.getenv())中尋找。通過啟用systemPropertiesMode和searchSystemEnvironment屬性,開發者能夠控制這一行為。

 

===========================================================================

 

  <context:property-placeholder>標簽提供了一種優雅的外在化參數配置的方式,不過該標簽在Spring配置文件中只能存在一份!!!

  眾所周知,Spring容器是采用反射掃描的發現機制,通過標簽的命名空間實例化實例,當Spring探測到容器中有一個org.springframework.beans.factory.config.PropertyPlaceholderCVonfigurer的Bean就會停止對剩余PropertyPlaceholderConfigurer的掃描,即只能存在一個實例!

<context:property-placeholder   
        location=""  
        file-encoding=""  
        ignore-resource-not-found=""  
        ignore-unresolvable=""  
        properties-ref=""  
        local-override=""  
        system-properties-mode=""  
        order=""  
/> 

  (1)location:表示屬性文件位置,多個之間通過如逗號/分號等分隔; 
  (2)file-encoding:文件編碼; 
  (3)ignore-resource-not-found:如果屬性文件找不到,是否忽略,默認false,即不忽略,找不到將拋出異常 
  (4)ignore-unresolvable:是否忽略解析不到的屬性,如果不忽略,找不到將拋出異常 
  (5)properties-ref:本地java.util.Properties配置 
  (6)local-override:是否本地覆蓋模式,即如果true,那么properties-ref的屬性將覆蓋location加載的屬性 
  (7)system-properties-mode:系統屬性模式,ENVIRONMENT(默認),NEVER,OVERRIDE 
  (8)ENVIRONMENT:將使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情況使用Spring 3.1之前的PropertyPlaceholderConfigurer 
  (9)OVERRIDE: PropertyPlaceholderConfigurer使用,因為在spring 3.1之前版本是沒有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment 
  (10)NEVER:只查找properties-ref、location; 
  (11)order:當配置多個<context:property-placeholder/>時的查找順序

使用注意: 

 1、location中的加載文件的順序

  如果location中有多個文件:

classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties

  將依次加載,值得注意的是如果后一個文件中有和前面某一個文件中屬性名是相同的,最終取的值是后加載的值 

  舉例來說: 
  default.properties文件中有個屬性名userId,其對應的值為-1 
  default2.properties文件中也有一個屬性名userId,其對應的值為-2 
  default3.properties文件中特有一個屬性名userId,其對於那個的值為-3

  default.properties文件先加載,此時userId的值為-1,當default3.properties文件加載時將更新原來的值,此時userId的值為-3,同理,最后加載default2.properties文件,所以userId最終值為-2 

  所以需要避免不同屬性文件中的屬性名稱重名

2.ignore-resource-not-found和ignore-unresolvable兩個屬性是類似的作用,推薦配對使用 

  如果location中的文件指向了一個不存在的文件,那么也極有可能意味着有屬性無法解析(雖然存在其他屬性文件中存在重名,但是這個是應該避免的) 
  所以當ignore-resource-not-found設為true時,ignore-unresolvable也必須設為true,其實當ignore-unresolvable設為true時,ignore-resource-not-found的值true或false,並不影響異常的拋出 
  如果設置為ture,后屬性值無法解析成功,將賦值為${屬性名} 
  不推薦將ignore-resource-not-found和ignore-unresolvable的值設置為ture,默認為false,可以有效避免程序運行異常

3.properties-ref屬性 

  引入其他方式引入的屬性文件

<bean id="refProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:default2.properties</value>
        </list>
    </property>
</bean>

  該屬性需要local-override配合使用,只有當local-override屬性值為true時,properties-ref屬性文件中屬性值將覆蓋location屬性文件屬性值(同名屬性)

4.local-override屬性 

  當local-override屬性值為true時,properties-ref屬性文件中屬性值將覆蓋location屬性文件屬性值(同名屬性)

5.sytem-properties-mode屬性 

  不同的sytem-properties-mode屬性定義了不同的查找順序 
  Environment環境:包括JDK環境,系統環境變量,Sevlet環境,Spring環境等,是Spring在3.1之后抽象的一個表示環境配置

  在local-override屬性值為false,sytem-properties-mode屬性值為ENVIRONMENT或OVRRIDE時,查找順序是location,然后是environment或者System.getProperty(),System.getenv()(Spring 3.1 之前) 
  即現加載location指向的屬性文件,再加之environment指向的環境,當environment環境中存在和location指向的屬性文件中同名的屬性,則該屬性的值將被修改,取決於environment環境中的值 
  如果sytem-properties-mode屬性值為NEVER,則只查詢location指向的屬性文件

  當local-override屬性值為true時,最后將加載properties-ref指向的文件,如遇到同名的,該同名屬性值將取決於properties-ref指向的文件中的值

  所以,最終程序中獲取的值將是一個綜合作用后的值,一般情況下建議sytem-properties-mode屬性值為NEVER避免ENVIRONMENT環境中的不可控


免責聲明!

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



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