Spring利用propertyConfigurer類 讀取.property數據庫配置文件


1.Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer類可以將.properties(key/value形式)文件中 
一些動態設定的值(value),在XML中替換為占位該鍵($key$)的值, 
.properties文件可以根據客戶需求,自定義一些相關的參數,這樣的設計可提供程序的靈活性。

2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部屬性文件,當然也可以指定外部文件的編碼,如: 
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
      <value>conf/sqlmap/jdbc.properties</value> 
    </property> 
     <property name="fileEncoding"> 
       <value>UTF-8</value> 
     </property> 
</bean> 
當然也可以引入多個屬性文件,如: 
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
   <property name="locations"> 
    <list> 
     <value>/WEB-INF/mail.properties</value>    
     <value>classpath: conf/sqlmap/jdbc.properties</value>//注意這兩種value值的寫法 
    </list> 
   </property> 
</bean>

基本的使用方法是: 
Xml代碼 
<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
        <value>classpath:/spring/include/dbQuery.properties</value> 
    </property> 
    <property name="fileEncoding"> 
       <value>UTF-8</value> 
     </property> 
</bean>

其中classpath是引用src目錄下的文件寫法。 
當存在多個Properties文件時,配置就需使用locations了: 
Xml代碼 
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
       <list> 
          <value>classpath:/spring/include/jdbc-parms.properties</value> 
          <value>classpath:/spring/include/base-config.properties</value> 
          <value>classpath*:config/jdbc.properties</value> 
        </list> 
    </property> 
</bean>


接下來我們要使用多個PropertyPlaceholderConfigurer來分散配置,達到整合多工程下的多個分散的Properties文件,其配置如下 
Xml代碼 
<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="order" value="1" /> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    <property name="location"> 
       <value>classpath:/spring/include/dbQuery.properties</value> 
    </property> 
</bean>


Xml代碼 
<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="order" value="2" /> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    <property name="locations"> 
      <list> 
        <value>classpath:/spring/include/jdbc-parms.properties</value> 
        <value>classpath:/spring/include/base-config.properties</value> 
      </list> 
    </property> 
</bean> 
其中order屬性代表其加載順序, 
而ignoreUnresolvablePlaceholders為是否忽略不可解析的Placeholder,如配置了多個PropertyPlaceholderConfigurer,則需設置為true

3.譬如,jdbc.properties的內容為: 
jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=round; 
jdbc.username=root 
jdbc.password=123456

4.那么在spring配置文件中,我們就可以這樣寫: 
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
   <property name="locations"> 
    <list> 
     <value>classpath: conf/sqlmap/jdbc.properties </value> 
    </list> 
   </property> 
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
   <property name="driverClassName" value="${jdbc.driverClassName}" /> 
   <property name="url" value="${jdbc.url}" /> 
   <property name="username" value="${jdbc.username}" /> 
   <property name="password" value="${jdbc.password}" /> 
</bean>

5.這樣,一個簡單的數據源就設置完畢了。可以看出:PropertyPlaceholderConfigurer起的作用就是將占位符指向的數據庫配置信息放在bean中定義的工具。


免責聲明!

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



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