---
title: "Spring中的四种声明式事务的配置（转）"
author: "istone"
date: "2013-11-02T19:31:33Z"
tags: []
categories: ["Web前端"]
slug: "spring"
draft: false
lastmod: "2013-11-02T19:32:39Z"
id: 1969232
url: "https://istone.iteye.com/blog/1969232"
---

Spring容器中有两种思想很重要，也就是我们常用的Ioc和Aop，如果理解了这两种思想， 对于我们学习设计模式和编程有很大的帮助，美国四人帮（GOF）写的设计模式中，有很多都用到了Ioc的思想。简单的说就是依赖注入的思想。常见的一种情 况：如果一个类中要复用另外一个类中的功能时，我们可能会首先想到继承，如果你知道Ioc这种思想的话，我想你不会用继承，你会马上想到把要用到功能抽取 出来，在我们要用到的类中只需通过set方法简单的注入就可以了，其实这里用到了对象的组合代替继承，这样不仅避免了单一继承，还很好的实现了松耦合。同 时也遵循了面向对象的编程的设计原则：多用组合，少用继承。在这里对于Ioc和Aop这两种思想的好处。我就不介绍了。接下来我要说的是Spring中几 种常见的事务配置，是Aop和Ioc的充分体现。  
       在说点题外话，以前EJB在J2EE中开发中可是大名鼎鼎的了。就是因为EJB提供了很多服务，而不需要我们去开发了，其中用到最多的算是它提供的声明式 事务了。在Spring没有出现之前，EJB在J2EE开发中扮演着非常重要的角色。同时也是很多项目失败的罪魁祸首。其中的原因就是因为它是重量级，强 侵入性收费的框架。需要昂贵的服务器支持，在加上它的测试更是让人头痛。而Spring恰恰与它相反。Spring是一个轻量级的，非侵入性的开源框架。 它提供的声明式事务的功能更是强大，不需要容器的支持。这一点吸引力很多人愿意放弃EJB而使用Spring。  
      让我们言归正传吧。  
       以下两个bean的配置是下面要用到的。

```
<!-- 定义事务管理器（声明式的事务） -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>
<!-- *******业务逻辑层(是对各个DAO层的正面封装)主要用到<<门面模式>>****** -->
 <bean id="fundService" class="com.jack.fund.service.serviceimpl.FundService">
  <property name="operdao">
   <ref bean="operatorDAO" />
  </property>
  <property name="producedao">
   <ref bean="fundProduceDAO" />
  </property>
  <property name="customerdao">
   <ref bean="customerDAO" />
  </property>
  <property name="accountdao">
   <ref bean="accountDAO" />
  </property>
  <property name="fundaccountdao">
   <ref bean="fundAccountDAO" />
  </property>
  <property name="fundtransdao">
   <ref bean="fundTransDAO" />
  </property>
 </bean>
```

   
可能还有其他很多模块。&lt;bean id="fundService"/&gt;可能只是其中的模块。  
       第一种：配置声明式事务的方法如下。也是我们最常用的方法了，它适用于你的库表比较少的情况下。

```
<bean id="fundServiceDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <!-- 配置事务管理器 -->
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <!-- 此属性指定目标类本省是否是代理的对象，如果目标类没有实现任何类，就设为true代表自己 -->
  <property name="proxyTargetClass">
   <value>false</value>
  </property>
  <property name="proxyInterfaces">
   <value>com.jack.fund.service.IFundService</value>
  </property>
  <!-- 目标bean -->
  <property name="target">
   <ref bean="fundService" />
  </property>
  <!-- 配置事务属性 -->
  <property name="transactionAttributes">
   <props>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop   key="find*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
```

   
 以下可能还有其他的xxxServiceDAOProxy.大家可以看出针对每一个功能模块配置一个业务代理服务。如果模块多大话，就显得代码有点多了，发现他们只是稍微一点不一样。这时我们就应该想到继承的思想。用第二种方法。  
  第二种：配置声明式事务的方法如下。这种情况适合相对比较多的模块时使用。

```
<!-- 利用继承的思想简化配置，要把abstract="true" -->
 <bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  lazy-init="true" abstract="true">
  <!-- 配置事务管理器 -->
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <!-- 配置事务属性 -->
  <property name="transactionAttributes">
   <props>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
```

   
而具体的模块可以简单的这样配置。只要指明它的parent（父类）就可以了。父类一般把abstract="true"，因为在容器加载的时候不需要初始化，等到用的时候再有它的子类调用的时候，再去初始化。

```
<bean id="fundServiceDAOProxy" parent="transactionBase" >
  <property name="target">
  <ref bean="fundService" />
  </property>
 </bean>
```

   
这样配置的话，如果有多个像fundService这样模块时，可以少些很多重复的代码。  
 第三种：配置声明式事务的方法如下。主要利用BeanNameAutoProxyCreator自动创建事务代理  
 

```
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <!-- 配置事务属性 -->
  <property name="transactionAttributes">
   <props>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
 <bean
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <list>
    <value>fundService</value>
   </list>
  </property>
  <property name="interceptorNames">
   <list>
    <value>transactionInterceptor</value>
   </list>
  </property>
 </bean>
```

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

```
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
<!-- 自动代理 -->
 <bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <!-- 可以是Service或DAO层（最好是针对业务层*Service） -->
  <property name="beanNames">
   <list>
    <value>*Service</value>
   </list>
  </property>
  <property name="interceptorNames">
   <list>
       <value>transactionInterceptor</value>
   </list>
  </property>
 </bean>
```

   
 自动代理还有一种用法就是结合正规表达式和advice使用。

```
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
 <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
<bean id="regexpMethodPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  <property name="advice">
  <ref bean="transactionInterceptor" />
  </property>
  <property name="pattern">
  <value>.*</value>
  </property>
  </bean>
```

   
这个方法可以针对具体的模块进行拦截并进行事务处理。

原文：http://blog.sina.com.cn/s/blog\_56fec5e10100ar1t.html