---
title: "Spring中的四种声明式事务的配置 "
author: "nieli"
date: "2009-11-27T16:49:31Z"
tags: ["AOP", "Bean", "Spring", "编程", "配置管理"]
slug: "spring"
draft: false
lastmod: "2009-11-27T16:49:31Z"
id: 531343
url: "https://nieli.iteye.com/blog/531343"
---

转自：[http://java.chinaitlab.com/Spring/744576.html](http://java.chinaitlab.com/Spring/744576.html)

spring的事务管理有两种方式：

1.声明式事务管理

*大多数Spring用户选择声明式事务管理。这是对应用代码影响最小的选择，因此也最符合 非侵入式 轻量级容器的理念。*

Spring的声明式事务管理是通过Spring AOP实现的，因为事务方面的代码与Spring绑定并以一种样板式风格使用，不过尽管如此，你一般并不需要理解AOP概念就可以有效地使用Spirng的声明式事务管理。

2.编程式事务管理

Spring提供两种方式的编程式事务管理：

- 使用 `TransactionTemplate`
- 直接使用一个 `PlatformTransactionManager` 实现

      如果你选择编程式事务管理，Spring小组推荐你采用第一种方法（即使用 `TransactionTemplate`）。第二种方法类似使用JTA的 `UserTransaction` API （除了异常处理简单点儿）。

当你只有很少的事务操作时，编程式事务管理通常比较合适。例如，如果你有一个Web应用，其中只有特定的更新操作有事务要求，你可能不愿使用Spring或其他技术设置事务代理。这种情况下，使用 `TransactionTemplate` *可能* 是个好办法。

     另一方面，如果你的应用中存在大量事务操作，那么声明式事务管理通常是值得的。它将事务管理与业务逻辑分离，而且在Spring中配置也不难。

 

 

下面讲的是声明式事务管理的四种声明方式：

 

Spring中的四种声明式事务的配置Spring容器中有两种思想很重要，也就是我们常用的Ioc和Aop，如果理解了这两种思想，对于我们学习设计模式和编程有很大的帮助，美国四人帮（GOF）写的设计模式中，有很多都用到了Ioc的思想。简单的说就是依赖注入的思想。常见的一种情况：如果一个类中要复用另外一个类中的功能时，我们可能会首先想到继承，如果你知道Ioc这种思想的话，我想你不会用继承，你会马上想到把要用到功能抽取出来，在我们要用到的类中只需通过set方法简单的注入就可以了，其实这里用到了对象的组合代替继承，这样不仅避免了单一继承，还很好的实现了松耦合。同时也遵循了面向对象的编程的设计原则：多用组合，少用继承。在这里对于Ioc和Aop这两种思想的好处。我就不介绍了。接下来我要说的是Spring中几种常见的事务配置，是Aop和Ioc的充分体现。

    在说点题外话，以前EJB在J2EE中开发中可是大名鼎鼎的了。就是因为EJB提供了很多服务，而不需要我们去开发了，其中用到最多的算是它提供的声明式事务了。在Spring没有出现之前，EJB在J2EE开发中扮演着非常重要的角色。同时也是很多项目失败的罪魁祸首。其中的原因就是因为它是重量级，强侵入性收费的框架。需要昂贵的[服务器](http://server.chinaitlab.com/)支持，在加上它的[测试](http://softtest.chinaitlab.com/)更是让人头痛。而Spring恰恰与它相反。Spring是一个轻量级的，非侵入性的开源框架。它提供的声明式事务的功能更是强大，不需要容器的支持。这一点吸引力很多人愿意放弃EJB而使用Spring.让我们言归正传吧。

    以下两个bean的配置是下面要用到的。

    &lt;!-- 定义事务管理器（声明式的事务） --&gt;  
     &lt;bean id="transactionManager"  
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
      &lt;property name="sessionFactory"&gt;  
       &lt;ref local="sessionFactory" /&gt;  
      &lt;/property&gt;  
     &lt;/bean&gt;  
    &lt;!-- \*\*\*\*\*\*\*业务逻辑层(是对各个DAO层的正面封装)主要用到&lt;&lt;门面模式&gt;&gt;\*\*\*\*\** --&gt;  
     &lt;bean id="fundService"  
      class="com.jack.fund.service.serviceimpl.FundService"&gt;  
      &lt;property name="operdao"&gt;  
       &lt;ref bean="operatorDAO" /&gt;  
      &lt;/property&gt;  
      &lt;property name="producedao"&gt;  
       &lt;ref bean="fundProduceDAO" /&gt;  
      &lt;/property&gt;  
      &lt;property name="customerdao"&gt;  
       &lt;ref bean="customerDAO" /&gt;  
      &lt;/property&gt;  
      &lt;property name="accountdao"&gt;  
       &lt;ref bean="accountDAO" /&gt;  
      &lt;/property&gt;  
      &lt;property name="fundaccountdao"&gt;  
       &lt;ref bean="fundAccountDAO" /&gt;  
      &lt;/property&gt;  
      &lt;property name="fundtransdao"&gt;  
       &lt;ref bean="fundTransDAO" /&gt;  
      &lt;/property&gt;  
     &lt;/bean&gt;  
    可能还有其他很多模块。&lt;bean id="fundService"/&gt;可能只是其中的模块。

    **第一种：配置声明式事务的方法如下**。也是我们最常用的方法了，它适用于你的库表比较少的情况下。  
    &lt;bean id="fundServiceDAOProxy"  
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;  
      &lt;!-- 配置事务管理器 --&gt;  
      &lt;property name="transactionManager"&gt;  
       &lt;ref bean="transactionManager" /&gt;  
      &lt;/property&gt;  
      &lt;!-- 此属性指定目标类本省是否是代理的对象，如果目标类没有实现任何类，就设为true代表自己 --&gt;  
      &lt;property name="proxyTargetClass"&gt;  
       &lt;value&gt;false&lt;/value&gt;  
      &lt;/property&gt;  
      &lt;property name="proxyInterfaces"&gt;  
       &lt;value&gt;com.jack.fund.service.IFundService&lt;/value&gt;  
      &lt;/property&gt;  
      &lt;!-- 目标bean --&gt;  
      &lt;property name="target"&gt;  
       &lt;ref bean="fundService" /&gt;  
      &lt;/property&gt;  
      &lt;!-- 配置事务属性 --&gt;  
      &lt;property name="transactionAttributes"&gt;  
       &lt;props&gt;  
        &lt;prop key="delete\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="add\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="update\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="save\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop   key="find\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
       &lt;/props&gt;  
      &lt;/property&gt;  
     &lt;/bean&gt;  
     以下可能还有其他的xxxServiceDAOProxy.大家可以看出针对每一个功能模块配置一个业务代理服务。如果模块多大话，就显得代码有点多了，发现他们只是稍微一点不一样。这时我们就应该想到继承的思想。用第二种方法。

     **第二种：配置声明式事务的方法如下**。这种情况适合相对比较多的模块时使用。  
    &lt;!-- 利用继承的思想简化配置，要把abstract="true" --&gt;  
     &lt;bean id="transactionBase"  
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
      lazy-init="true" abstract="true"&gt;  
      &lt;!-- 配置事务管理器 --&gt;  
      &lt;property name="transactionManager"&gt;  
       &lt;ref bean="transactionManager" /&gt;  
      &lt;/property&gt;  
      &lt;!-- 配置事务属性 --&gt;  
      &lt;property name="transactionAttributes"&gt;  
       &lt;props&gt;  
        &lt;prop key="delete\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="add\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="update\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="save\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="find\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
       &lt;/props&gt;  
      &lt;/property&gt;  
     &lt;/bean&gt;  
    而具体的模块可以简单的这样配置。只要指明它的parent（父类）就可以了。父类一般把abstract="true"，因为在容器加载的时候不需要初始化，等到用的时候再有它的子类调用的时候，再去初始化。  
    &lt;bean id="fundServiceDAOProxy" parent="transactionBase" &gt;  
      &lt;property name="target"&gt;  
      &lt;ref bean="fundService" /&gt;  
      &lt;/property&gt;  
     &lt;/bean&gt;  
    这样配置的话，如果有多个像fundService这样模块时，可以少些很多重复的代码。

**第三种：配置声明式事务的方法如下**。主要利用BeanNameAutoProxyCreator自动创建事务代理  
      &lt;bean id="transactionInterceptor"  
      class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;  
      &lt;property name="transactionManager"&gt;  
       &lt;ref bean="transactionManager" /&gt;  
      &lt;/property&gt;  
      &lt;!-- 配置事务属性 --&gt;  
      &lt;property name="transactionAttributes"&gt;  
       &lt;props&gt;  
        &lt;prop key="delete\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="add\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="update\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="save\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
        &lt;prop key="find\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
       &lt;/props&gt;  
      &lt;/property&gt;  
     &lt;/bean&gt;  
     &lt;bean  
      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;  
      &lt;property name="beanNames"&gt;  
       &lt;list&gt;  
        &lt;value&gt;fundService&lt;/value&gt;  
       &lt;/list&gt;  
      &lt;/property&gt;  
      &lt;property name="interceptorNames"&gt;  
       &lt;list&gt;  
        &lt;value&gt;transactionInterceptor&lt;/value&gt;  
       &lt;/list&gt;  
      &lt;/property&gt;  
     &lt;/bean&gt;  
    这种方法主要利用了拦截器的原理。

    前三种方法一般都必需指定具体的模块bean.如果模块过多话，比如一个大型的网站一般有几十个模块。我们就得考虑用第四种的配置方式了。自动创建事务代理的方式了。

 

    **第四种：配置声明式事务的方法如下。**  
    &lt;bean id="transactionInterceptor"  
      class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;  
      &lt;property name="transactionManager"&gt;  
       &lt;ref bean="transactionManager" /&gt;  
      &lt;/property&gt;  
    &lt;!-- 自动代理 --&gt;  
     &lt;bean id="autoproxy"  
      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;  
      &lt;!-- 可以是Service或DAO层（最好是针对业务层\*Service） --&gt;  
      &lt;property name="beanNames"&gt;  
       &lt;list&gt;  
        &lt;value&gt;\*Service&lt;/value&gt;  
       &lt;/list&gt;  
      &lt;/property&gt;  
      &lt;property name="interceptorNames"&gt;  
       &lt;list&gt;  
           &lt;value&gt;transactionInterceptor&lt;/value&gt;  
       &lt;/list&gt;  
      &lt;/property&gt;  
     &lt;/bean&gt;  
     自动代理还有一种用法就是结合正规表达式和advice使用。  
    &lt;bean id="transactionInterceptor"  
      class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;  
      &lt;property name="transactionManager"&gt;  
       &lt;ref bean="transactionManager" /&gt;  
      &lt;/property&gt;  
     &lt;bean id="autoProxyCreator"  
      class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /&gt;  
    &lt;bean id="regexpMethodPointcutAdvisor"  
      class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"&gt;  
      &lt;property name="advice"&gt;  
      &lt;ref bean="transactionInterceptor" /&gt;  
      &lt;/property&gt;  
      &lt;property name="pattern"&gt;  
      &lt;value&gt;.\*&lt;/value&gt;  
      &lt;/property&gt;  
      &lt;/bean&gt;  
      这个方法可以针对具体的模块进行拦截并进行事务处理。在你的实际项目中，你可以根据你的情况选用不同的方法。