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&characterEncoding=UTF-8&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中定義的工具。
-
<!-- dataSource -->
-
<bean id="dataSource"
-
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-
<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>
-
<!-- sessionFactory -->
-
<bean id="sessionFactory"
-
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
-
<property name="dataSource" ref="dataSource" />
-
<property name="mappingResources">
-
<list>
-
<value>cn/xg/hibernate/spring/User.hbm.xml</value><!--這裏的映射路徑問題,這種方法只能一個一個加-->
-
<value>cn/xg/hibernate/spring/Group.hbm.xml</value>
-
</list>
-
<!-- 加載一個路徑下的*.hbm.xml文件方法:
-
<property name="mappingDirectoryLocations">
-
<list>
-
<value>classpath:/cn/xg/spring/model</value>
-
</list>
-
</property>
-
-->
-
</property>
-
<property name="hibernateProperties">
-
<props>
-
<prop key="hibernate.dialect">
-
${hibernate.dialect}
-
</prop>
-
<prop key="hibernate.show_sql">true</prop>
-
</props>
-
</property>
-
</bean>
-
<!-- DAO實現類extends HibernateDaoSupport,注入sessionFactory -->
-
<bean id="userMgrImpl" class="cn.xg.hibernate.spring.UserMgrImpl">
-
<property name="sessionFactory" ref="sessionFactory" />
-
</bean>
-
-
<bean id="groupMgrImpl"
-
class="cn.xg.hibernate.spring.GroupMgrImpl">
-
<property name="sessionFactory" ref="sessionFactory" />
-
<property name="userImpl" ref="userMgrImpl"/>
-
<property name="transactionTemplate" ref="transactionTemplate"/>
-
</bean>
-
<!-- 事務管理 -->
-
<bean id="transactionManager"
-
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
-
<property name="sessionFactory" ref="sessionFactory" />
-
</bean>
-
<!-- 編程式事務的寫法 :向Dao實現類中注入transactionTemplate,調動其execute()方法,接口回調new TransactionCallback()-->
-
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
-
<property name="transactionManager" ref="transactionManager"/>
-
</bean>
-
-
-
<!-- 聲時式事務第一種寫法 -->
-
<!--
-
<bean id="groupMgr"
-
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
-
<property name="transactionManager" ref="transactionManager" />
-
<property name="target" ref="groupMgrImpl" />
-
<property name="transactionAttributes">
-
<props>
-
<prop key="add*">PROPAGATION_REQUIRED</prop>
-
<prop key="get*">PROPAGATION_REQUIRED</prop>
-
<prop key="*">readOnly</prop>
-
</props>
-
</property>
-
</bean>
-
-->
-
<!-- 聲時式事務第二種寫法 -->
-
<!-- 事務的傳播特性
-
-
<tx:advice id="txAdvice">
-
<tx:attributes>
-
<tx:method name="add*" propagation="REQUIRED" />
-
<tx:method name="get*" propagation="REQUIRED" />
-
<tx:method name="*" read-only="true" />
-
</tx:attributes>
-
</tx:advice>
-
<aop:config>
-
<aop:advisor pointcut="execution(* cn.xg.hibernate.spring.*.*(..))"
-
advice-ref="txAdvice" />
-
</aop:config>
-
-->
-
-
</beans>
-
-
jdbc.properties
-
-
jdbc.driverClassName=com.mysql.jdbc.Driver
-
jdbc.url=jdbc:mysql://localhost:3306/數據庫名
-
jdbc.username=數據庫用戶名
-
jdbc.password=數據庫密碼
-
hibernate.dialect=org.hibernate.dialect.MySQLDialect(方言.這裏是MySql)
參考文章:
http://hi.baidu.com/seashell752/blog/item/2764310e2f35f2e137d1225f.html
http://hi.baidu.com/suny_duan/blog/item/e8e9b2a5f31d5efc9052ee8b.html