第一種:每一個Bean都有一個代理: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="root" /> <!-- 鏈接池啓動時的初始值 --> <property name="initialSize" value="10" /> <!-- 鏈接池的最大值 --> <property name="maxActive" value="10" /> <!-- 最大空閒值.當通過一個高峯時間後,鏈接池能夠慢慢將已經用不到的鏈接慢慢釋放一部分,一直減小到maxIdle爲止 --> <property name="maxIdle" value="20" /> <!-- 最小空閒值.當空閒的鏈接數少於閥值時,鏈接池就會預申請去一些鏈接,以避免洪峯來時來不及申請 --> <property name="minIdle" value="10" /> <property name="defaultAutoCommit" value="true" /> </bean> <!-- 會話工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingLocations"> <list> <value>classpath:/com/nms/entity/**/*.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <!-- 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置服務層 --> <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="userDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!-- 配置事務管理器 --> <property name="transactionManager" ref="transactionManager" /> <property name="target" ref="userDaoAgency" /> <property name="proxyInterfaces" value="com.dao.UserDao" /> <!-- 配置事務屬性 --> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans> 第二種:全部Bean共享一個代理 <?xml version="1.0" encoding="UTF-8"?> <beans> <!-- 數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- // 配置同上 --> </bean> <!-- 會話工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- // 配置同上 --> </bean> <!-- 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 定義事務 --> <bean id="base" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <!-- 配置事務管理器 --> <property name="transactionManager" ref="transactionManager" /> <!-- 配置事務屬性 --> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 配置服務層 --> <bean id="userDao" class="com.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 代理對象 --> <bean id="userDaoAgency" parent="base"> <property name="target" ref="userDao" /> </bean> </beans> 第三種:攔截器: <?xml version="1.0" encoding="UTF-8"?> <beans> <!-- 數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- // 配置同上 --> </bean> <!-- 會話工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- // 配置同上 --> </bean> <!-- 定義事務管理器(聲明式的事務) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 定義事務 --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager" /> <!-- 配置事務屬性 --> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*DaoImpl</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> <!-- 配置服務層 --> <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans> 第四種:使用tx標籤配置的攔截器: <?xml version="1.0" encoding="UTF-8"?> <beans> <!-- 數據源 --> <bean id="dataSource"> <!-- // 配置同上 --> </bean> <!-- 會話工廠 --> <bean id="sessionFactory"> <!-- // 配置同上 --> </bean> <context:annotation-config /> <context:component-scan base-package="com.dao" /> <!-- 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 定義事務 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 定義切面 --> <aop:config> <aop:pointcut id="interceptorPointCuts" expression="execution(* com.dao.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" /> </aop:config> </beans> 第五種:註解: <?xml version="1.0" encoding="UTF-8"?> <beans> <!-- 數據源 --> <bean id="dataSource"> <!-- // 配置同上 --> </bean> <!-- 會話工廠 --> <bean id="sessionFactory"> <!-- // 配置同上 --> </bean> <context:annotation-config /> <!-- 使用註解的包路徑 --> <context:component-scan base-package="com.dao" /> <!-- 支持 @Transactional 標記 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
若是使用了註解,那麼實現類應該這樣寫: @Transactional @Component("userDaoAgency") public class UserDaoImpl extends HibernateDaoSupport implements UserDao { /** * 爲方法增長事務處理特性 */ @Transactional(readOnly=true) public void getUser(){ } }