Spring propertyConfigurer類

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

 

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

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>spring

基本的使用方法是:
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>sql

其中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>apache


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>url

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

相關文章
相關標籤/搜索