---
title: "spring中配置事务的两种方法"
author: "lin5061"
date: "2007-05-30T08:08:26Z"
tags: []
slug: "spring"
draft: false
lastmod: "2009-07-17T22:36:26Z"
id: 88285
url: "https://lin5061.iteye.com/blog/88285"
---

在spring1.2及以前,用的[安定北京](http://www.andingbeijing.com "安定北京")是spring的代理类来配置事务的,如:[安定北京](http://www.andingbeijing.com "安定北京")  
 &lt;bean id="matchAllTxInterceptor"  
  class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;  
  &lt;property name="transactionManager"&gt;  
   &lt;ref bean="transactionManager" /&gt;  
  &lt;/property&gt;  
  &lt;property name="transactionAttributes"&gt;  
   &lt;props&gt;  
    &lt;prop key="find\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
    &lt;prop key="get\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
    &lt;prop key="\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
   &lt;/props&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

 

 &lt;bean id="autoProxyCreator"  
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;  
  &lt;property name="interceptorNames"&gt;  
   &lt;list&gt;  
    &lt;idref local="matchAllTxInterceptor" /&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
  &lt;property name="proxyTargetClass"&gt;  
   &lt;value&gt;true&lt;/value&gt;  
  &lt;/property&gt;  
  &lt;property name="beanNames"&gt;  
   &lt;list&gt;  
    &lt;value&gt;\*Service&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

 

但是在spring2.0出来后,支持了aop式的事务配置,如:  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
    xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
    xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)"  
    xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx)"  
    xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)  
           [http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) [http://www.springframework.org/schema/aop/spring-aop-2.0.xsd](http://www.springframework.org/schema/aop/spring-aop-2.0.xsd)  
           [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-2.0.xsd](http://www.springframework.org/schema/tx/spring-tx-2.0.xsd)"  
    default-autowire="byName" default-lazy-init="true"&gt;

 

 &lt;!-- 事务管理 --&gt;  
 &lt;!-- 支持 @Transactional 标记 --&gt;  
 &lt;tx:annotation-driven/&gt;

 

 &lt;!-- 支持 @AspectJ 标记--&gt;  
 &lt;aop:aspectj-autoproxy/&gt;

 

 &lt;!-- 以AspectJ方式 定义 AOP --&gt;  
 &lt;aop:config proxy-target-class="false"&gt;  
  &lt;aop:advisor pointcut="execution(* com.sitechasia.xinnet..service..\*ServiceImpl.\*(..))"  
      advice-ref="txAdvice"/&gt;  
 &lt;/aop:config&gt;

 

 &lt;!-- 基本事务定义,使用transactionManager作事务管理,默认get\*方法的事务为readonly,其余方法按默认设置.  
    默认的设置请参考Spring文档事务一章. --&gt;  
 &lt;tx:advice id="txAdvice"&gt;  
  &lt;tx:attributes&gt;  
   &lt;tx:method name="get\*" propagation="SUPPORTS" read-only="true"/&gt;  
   &lt;tx:method name="find\*" propagation="SUPPORTS" read-only="true"/&gt;  
   &lt;tx:method name="open\*" propagation="REQUIRED" /&gt;  
   &lt;tx:method name="add\*" propagation="REQUIRED" /&gt;  
   &lt;tx:method name="delete\*" propagation="REQUIRED" /&gt;  
   &lt;tx:method name="upgrade\*" propagation="REQUIRED" /&gt;  
   &lt;tx:method name="\*" propagation="REQUIRED"/&gt;  
  &lt;/tx:attributes&gt;  
 &lt;/tx:advice&gt;  
&lt;/beans&gt;  
这种配置在springside里有体现.

 

但是不管是哪种配置方法,请尽量避免各种类之间的循环注入,不然的话,加上事务后会抛出org.springframework.beans.factory.BeanCurrentlyInCreationException…… in its raw version as part of a circular reference, but has eventually been wrapped (for example as part of auto-proxy creation). This means that said other beans do not use the final……种错误.  
举个例子:如果A需要注入B,B也需要注入A,那么不配置事务的时候不会出现问题,相互注入能通过,但是配置事务后就有问题了.  
其实想想,这样的相互引用是不合理的,试想,如果A和B在不同的包下,打包的时候A必需在B的基础上才能打,B必需在A的基础上才能打,那结果是两个包都打不成[安定北京](http://www.andingbeijing.com "安定北京"),除非用源代码才能打得出这样的包.[安定北京](http://www.andingbeijing.com "安定北京")