一。Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。
DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为 HibernateTransactionManager
根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下
第一种方式:每个Bean都有一个代理
Java代码
1.<bean id="sessionFactory"
2. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
3. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
4. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
5. </bean>
6.
7. <!-- 定义事务管理器(声明式的事务) -->
8. <bean id="transactionManager"
9. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
10. <property name="sessionFactory" ref="sessionFactory" />
11. </bean>
12.
13. <!-- 配置DAO -->
14. <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
15. <property name="sessionFactory" ref="sessionFactory" />
16. </bean>
17.
18. <bean id="userDao"
19. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
20. <!-- 配置事务管理器 -->
21. <property name="transactionManager" ref="transactionManager" />
22. <property name="target" ref="userDaoTarget" />
23. <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
24. <!-- 配置事务属性 -->
25. <property name="transactionAttributes">
26. <props>
27. <prop key="*">PROPAGATION_REQUIRED</prop>
28. </props>
29. </property>
30. </bean>
31.lt;/beans>
第二种方式:所有Bean共享一个代理基类
Java代码
1.<bean id="sessionFactory"
2. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
3. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
4. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
5. </bean>
6.
7. <!-- 定义事务管理器(声明式的事务) -->
8. <bean id="transactionManager"
9. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
10. <property name="sessionFactory" ref="sessionFactory" />
11. </bean>
12.
13. <bean id="transactionBase"
14. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
15. lazy-init="true" abstract="true">
16. <!-- 配置事务管理器 -->
17. <property name="transactionManager" ref="transactionManager" />
18. <!-- 配置事务属性 -->
19. <property name="transactionAttributes">
20. <props>
21. <prop key="*">PROPAGATION_REQUIRED</prop>
22. </props>
23. </property>
24. </bean>
25.
26. <!-- 配置DAO -->
27. <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
28. <property name="sessionFactory" ref="sessionFactory" />
29. </bean>
30.
31. <bean id="userDao" parent="transactionBase" >
32. <property name="target" ref="userDaoTarget" />
33. </bean>
34.</beans>
第三种方式:使用拦截器
Java代码
1.<bean id="sessionFactory"
2. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
3. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
4. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
5. </bean>
6.
7. <!-- 定义事务管理器(声明式的事务) -->
8. <bean id="transactionManager"
9. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
10. <property name="sessionFactory" ref="sessionFactory" />
11. </bean>
12.
13. <bean id="transactionInterceptor"
14. class="org.springframework.transaction.interceptor.TransactionInterceptor">
15. <property name="transactionManager" ref="transactionManager" />
16. <!-- 配置事务属性 -->
17. <property name="transactionAttributes">
18. <props>
19. <prop key="*">PROPAGATION_REQUIRED</prop>
20. </props>
21. </property>
22. </bean>
23.
24. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
25. <property name="beanNames">
26. <list>
27. <value>*Dao</value>
28. </list>
29. </property>
30. <property name="interceptorNames">
31. <list>
32. <value>transactionInterceptor</value>
33. </list>
34. </property>
35. </bean>
36.
37. <!-- 配置DAO -->
38. <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
39. <property name="sessionFactory" ref="sessionFactory" />
40. </bean>
41.</beans>
第四种方式:使用tx标签配置的拦截器
Java代码
1.<context:annotation-config />
2. <context:component-scan base-package="com.bluesky" />
3.
4. <bean id="sessionFactory"
5. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
6. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
7. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
8. </bean>
9.
10. <!-- 定义事务管理器(声明式的事务) -->
11. <bean id="transactionManager"
12. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
13. <property name="sessionFactory" ref="sessionFactory" />
14. </bean>
15.
16. <tx:advice id="txAdvice" transaction-manager="transactionManager">
17. <tx:attributes>
18. <tx:method name="*" propagation="REQUIRED" />
19. </tx:attributes>
20. </tx:advice>
21.
22. <aop:config>
23. <aop:pointcut id="interceptorPointCuts"
24. expression="execution(* com.bluesky.spring.dao.*.*(..))" />
25. <aop:advisor advice-ref="txAdvice"
26. pointcut-ref="interceptorPointCuts" />
27. </aop:config>
28.</beans>
第五种方式:全注解
Java代码
1.<context:annotation-config />
2. <context:component-scan base-package="com.bluesky" />
3.
4. <tx:annotation-driven transaction-manager="transactionManager"/>
5.
6. <bean id="sessionFactory"
7. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
8. <property name="configLocation" value="classpath:hibernate.cfg.xml" />
9. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
10. </bean>
11.
12. <!-- 定义事务管理器(声明式的事务) -->
13. <bean id="transactionManager"
14. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
15. <property name="sessionFactory" ref="sessionFactory" />
16. </bean>
17.
18.</beans>
<context:annotation-config /> <context:component-scan base-package="com.bluesky" /> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>此时在DAO上需加上@Transactional注解,如下:
Java代码
1.package com.bluesky.spring.dao;
2.
3.import java.util.List;
4.
5.import org.hibernate.SessionFactory;
6.import org.springframework.beans.factory.annotation.Autowired;
7.import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
8.import org.springframework.stereotype.Component;
9.
10.import com.bluesky.spring.domain.User;
11.
12.@Transactional
13.@Component("userDao")
14.public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
15.
16. public List<User> listUsers() {
17. return this.getSession().createQuery("from User").list();
18. }
19.
20.
21.}