Spring 利用PropertyPlaceholderConfigurer佔位符

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




Java代碼  收藏代碼
  1. <!-- dataSource -->  
  2. <bean id="dataSource"  
  3.    class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  4.    <property name="driverClassName"  
  5.     value="${jdbc.driverClassName}" />  
  6.    <property name="url" value="${jdbc.url}" />  
  7.    <property name="username" value="${jdbc.username}" />  
  8.    <property name="password" value="${jdbc.password}" />  
  9. </bean>  
  10. <!-- sessionFactory -->  
  11. <bean id="sessionFactory"  
  12.    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  13.    <property name="dataSource" ref="dataSource" />  
  14.    <property name="mappingResources">  
  15.     <list>  
  16.      <value>cn/xg/hibernate/spring/User.hbm.xml</value><!--這裏的映射路徑問題,這種方法只能一個一個加-->  
  17.      <value>cn/xg/hibernate/spring/Group.hbm.xml</value>  
  18.     </list>  
  19.     <!-- 加載一個路徑下的*.hbm.xml文件方法:  
  20.      <property name="mappingDirectoryLocations">  
  21.      <list>  
  22.      <value>classpath:/cn/xg/spring/model</value>  
  23.      </list>  
  24.      </property>  
  25.     -->  
  26.    </property>  
  27.    <property name="hibernateProperties">  
  28.     <props>  
  29.      <prop key="hibernate.dialect">  
  30.       ${hibernate.dialect}  
  31.      </prop>  
  32.      <prop key="hibernate.show_sql">true</prop>  
  33.     </props>  
  34.    </property>  
  35. </bean>  
  36. <!-- DAO實現類extends HibernateDaoSupport,注入sessionFactory -->  
  37. <bean id="userMgrImpl" class="cn.xg.hibernate.spring.UserMgrImpl">  
  38.    <property name="sessionFactory" ref="sessionFactory" />  
  39. </bean>  
  40.   
  41. <bean id="groupMgrImpl"  
  42.    class="cn.xg.hibernate.spring.GroupMgrImpl">  
  43.    <property name="sessionFactory" ref="sessionFactory" />  
  44.    <property name="userImpl" ref="userMgrImpl"/>  
  45.    <property name="transactionTemplate" ref="transactionTemplate"/>  
  46. </bean>  
  47. <!-- 事務管理 -->  
  48. <bean id="transactionManager"  
  49.    class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  50.    <property name="sessionFactory" ref="sessionFactory" />  
  51. </bean>  
  52. <!-- 編程式事務的寫法 :向Dao實現類中注入transactionTemplate,調動其execute()方法,接口回調new TransactionCallback()-->  
  53. <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">  
  54.    <property name="transactionManager" ref="transactionManager"/>  
  55. </bean>  
  56.   
  57.   
  58. <!-- 聲時式事務第一種寫法 -->  
  59. <!--  
  60.    <bean id="groupMgr"  
  61.    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  62.    <property name="transactionManager" ref="transactionManager" />  
  63.    <property name="target" ref="groupMgrImpl" />  
  64.    <property name="transactionAttributes">  
  65.    <props>  
  66.    <prop key="add*">PROPAGATION_REQUIRED</prop>  
  67.    <prop key="get*">PROPAGATION_REQUIRED</prop>  
  68.    <prop key="*">readOnly</prop>  
  69.    </props>  
  70.    </property>  
  71.    </bean>  
  72. -->   
  73. <!-- 聲時式事務第二種寫法 -->  
  74. <!-- 事務的傳播特性  
  75.   
  76. <tx:advice id="txAdvice">  
  77.    <tx:attributes>  
  78.     <tx:method name="add*" propagation="REQUIRED" />  
  79.     <tx:method name="get*" propagation="REQUIRED" />  
  80.     <tx:method name="*" read-only="true" />  
  81.    </tx:attributes>  
  82. </tx:advice>  
  83. <aop:config>  
  84.    <aop:advisor pointcut="execution(* cn.xg.hibernate.spring.*.*(..))"  
  85.     advice-ref="txAdvice" />  
  86. </aop:config>  
  87. -->  
  88.   
  89. </beans>  
  90.   
  91. jdbc.properties  
  92.   
  93. jdbc.driverClassName=com.mysql.jdbc.Driver  
  94. jdbc.url=jdbc:mysql://localhost:3306/數據庫名  
  95. jdbc.username=數據庫用戶名  
  96. jdbc.password=數據庫密碼  
  97. hibernate.dialect=org.hibernate.dialect.MySQLDialect(方言.這裏是MySql) 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章