天天看點

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中定義的工具。

繼續閱讀