---
title: "spring事务管理几种方式（转）"
author: "durbao"
date: "2016-07-14T17:52:47Z"
tags: []
categories: ["研发管理"]
slug: "spring"
draft: false
lastmod: "2016-07-14T17:52:46Z"
id: 2310737
url: "https://durbao.iteye.com/blog/2310737"
---

前段时间对Spring的事务配置做了比较深入的研究，在此之间对Spring的事务配置虽说也配置过，但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清，还是比较好掌握的。

    总结如下：

    Spring配置文件中关于事务配置总是由三个组成部分，分别是DataSource、TransactionManager和代理机制这三部分，无论哪种配置方式，一般变化的只是代理机制这部分。

    DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化，比如使用Hibernate进行数据访问时，DataSource实际为SessionFactory，TransactionManager的实现为HibernateTransactionManager。

具体如下图：  
![](http://dl.iteye.com/upload/attachment/517752/1a809961-f573-30ef-9b93-e83bb61d2fca.jpg)  
  根据代理机制的不同，总结了五种Spring事务的配置方式，配置文件如下：

    第一种方式：每个Bean都有一个代理

Xml代码     [![收藏代码](http://lzh166.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xmlns:aop="http://www.springframework.org/schema/aop"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
08. http://www.springframework.org/schema/context
09. http://www.springframework.org/schema/context/spring-context-2.5.xsd
10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&gt;
11. &lt;bean id="sessionFactory"
12. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
13. &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
14. &lt;property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /&gt;
15. &lt;/bean&gt;
16. &lt;!-- 定义事务管理器（声明式的事务） --&gt;
17. &lt;bean id="transactionManager"
18. class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
19. &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
20. &lt;/bean&gt;
21. &lt;!-- 配置DAO --&gt;
22. &lt;bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"&gt;
23. &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
24. &lt;/bean&gt;
25. &lt;bean id="userDao"
26. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;
27. &lt;!-- 配置事务管理器 --&gt;
28. &lt;property name="transactionManager" ref="transactionManager" /&gt;
29. &lt;property name="target" ref="userDaoTarget" /&gt;
30. &lt;property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" /&gt;
31. &lt;!-- 配置事务属性 --&gt;
32. &lt;property name="transactionAttributes"&gt;
33. &lt;props&gt;
34. &lt;prop key="\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;
35. &lt;/props&gt;
36. &lt;/property&gt;
37. &lt;/bean&gt;
38. &lt;/beans&gt;

 第二种方式：所有Bean共享一个代理基类

Xml代码     [![收藏代码](http://lzh166.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xmlns:aop="http://www.springframework.org/schema/aop"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
08. http://www.springframework.org/schema/context
09. http://www.springframework.org/schema/context/spring-context-2.5.xsd
10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&gt;
11. &lt;bean id="sessionFactory"
12. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
13. &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
14. &lt;property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /&gt;
15. &lt;/bean&gt;
16. &lt;!-- 定义事务管理器（声明式的事务） --&gt;
17. &lt;bean id="transactionManager"
18. class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
19. &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
20. &lt;/bean&gt;
21. &lt;bean id="transactionBase"
22. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
23. lazy-init="true" abstract="true"&gt;
24. &lt;!-- 配置事务管理器 --&gt;
25. &lt;property name="transactionManager" ref="transactionManager" /&gt;
26. &lt;!-- 配置事务属性 --&gt;
27. &lt;property name="transactionAttributes"&gt;
28. &lt;props&gt;
29. &lt;prop key="\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;
30. &lt;/props&gt;
31. &lt;/property&gt;
32. &lt;/bean&gt;
33. &lt;!-- 配置DAO --&gt;
34. &lt;bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"&gt;
35. &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
36. &lt;/bean&gt;
37. &lt;bean id="userDao" parent="transactionBase" &gt;
38. &lt;property name="target" ref="userDaoTarget" /&gt;
39. &lt;/bean&gt;
40. &lt;/beans&gt;

 

第三种方式：使用拦截器

Xml代码     [![收藏代码](http://lzh166.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xmlns:aop="http://www.springframework.org/schema/aop"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
08. http://www.springframework.org/schema/context
09. http://www.springframework.org/schema/context/spring-context-2.5.xsd
10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&gt;
11. &lt;bean id="sessionFactory"
12. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
13. &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
14. &lt;property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /&gt;
15. &lt;/bean&gt;
16. &lt;!-- 定义事务管理器（声明式的事务） --&gt;
17. &lt;bean id="transactionManager"
18. class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
19. &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
20. &lt;/bean&gt;
21. &lt;bean id="transactionInterceptor"
22. class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;
23. &lt;property name="transactionManager" ref="transactionManager" /&gt;
24. &lt;!-- 配置事务属性 --&gt;
25. &lt;property name="transactionAttributes"&gt;
26. &lt;props&gt;
27. &lt;prop key="\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;
28. &lt;/props&gt;
29. &lt;/property&gt;
30. &lt;/bean&gt;
31. &lt;bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;
32. &lt;property name="beanNames"&gt;
33. &lt;list&gt;
34. &lt;value&gt;\*Dao&lt;/value&gt;
35. &lt;/list&gt;
36. &lt;/property&gt;
37. &lt;property name="interceptorNames"&gt;
38. &lt;list&gt;
39. &lt;value&gt;transactionInterceptor&lt;/value&gt;
40. &lt;/list&gt;
41. &lt;/property&gt;
42. &lt;/bean&gt;
43. &lt;!-- 配置DAO --&gt;
44. &lt;bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl"&gt;
45. &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
46. &lt;/bean&gt;
47. &lt;/beans&gt;

 

第四种方式：使用tx标签配置的拦截器

Xml代码     [![收藏代码](http://lzh166.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xmlns:aop="http://www.springframework.org/schema/aop"
06. xmlns:tx="http://www.springframework.org/schema/tx"
07. xsi:schemaLocation="http://www.springframework.org/schema/beans
08. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
09. http://www.springframework.org/schema/context
10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;
13. &lt;context:annotation-config /&gt;
14. &lt;context:component-scan base-package="com.bluesky" /&gt;
15. &lt;bean id="sessionFactory"
16. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
17. &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
18. &lt;property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /&gt;
19. &lt;/bean&gt;
20. &lt;!-- 定义事务管理器（声明式的事务） --&gt;
21. &lt;bean id="transactionManager"
22. class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
23. &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
24. &lt;/bean&gt;
25. &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;
26. &lt;tx:attributes&gt;
27. &lt;tx:method name="\*" propagation="REQUIRED" /&gt;
28. &lt;/tx:attributes&gt;
29. &lt;/tx:advice&gt;
30. &lt;aop:config&gt;
31. &lt;aop:pointcut id="interceptorPointCuts"
32. expression="execution(* com.bluesky.spring.dao.\*.\*(..))" /&gt;
33. &lt;aop:advisor advice-ref="txAdvice"
34. pointcut-ref="interceptorPointCuts" /&gt;
35. &lt;/aop:config&gt;
36. &lt;/beans&gt;

 

第五种方式：全注解

Xml代码     [![收藏代码](http://lzh166.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xmlns:aop="http://www.springframework.org/schema/aop"
06. xmlns:tx="http://www.springframework.org/schema/tx"
07. xsi:schemaLocation="http://www.springframework.org/schema/beans
08. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
09. http://www.springframework.org/schema/context
10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;
13. &lt;context:annotation-config /&gt;
14. &lt;context:component-scan base-package="com.bluesky" /&gt;
15. &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
16. &lt;bean id="sessionFactory"
17. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
18. &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
19. &lt;property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /&gt;
20. &lt;/bean&gt;
21. &lt;!-- 定义事务管理器（声明式的事务） --&gt;
22. &lt;bean id="transactionManager"
23. class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
24. &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
25. &lt;/bean&gt;
26. &lt;/beans&gt;

 

此时在DAO上需加上@Transactional注解，如下

Java代码     [![收藏代码](http://lzh166.iteye.com/images/icon_star.png)]()

01. package com.bluesky.spring.dao;
02. import java.util.List;
03. import org.hibernate.SessionFactory;
04. import org.springframework.beans.factory.annotation.Autowired;
05. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
06. import org.springframework.stereotype.Component;
07. import com.bluesky.spring.domain.User;
08. @Transactional
09. @Component("userDao")
10. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
11. public List&lt;User&gt; listUsers() {
12. return this.getSession().createQuery("from User").list();
13. }
14. }