ITeye博客

spring中配置事务的两种方法

lin5061 · 2007-05-30 08:08 · Java · Markdown

在spring1.2及以前,用的安定北京是spring的代理类来配置事务的,如:安定北京
 <bean id="matchAllTxInterceptor"
  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>

 

 <bean id="autoProxyCreator"
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="interceptorNames">
   <list>
    <idref local="matchAllTxInterceptor" />
   </list>
  </property>
  <property name="proxyTargetClass">
   <value>true</value>
  </property>
  <property name="beanNames">
   <list>
    <value>*Service</value>
   </list>
  </property>
 </bean>

 

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

 

 <!-- 事务管理 -->
 <!-- 支持 @Transactional 标记 -->
 <tx:annotation-driven/>

 

 <!-- 支持 @AspectJ 标记-->
 <aop:aspectj-autoproxy/>

 

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

 

 <!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.
    默认的设置请参考Spring文档事务一章. -->
 <tx:advice id="txAdvice">
  <tx:attributes>
   <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
   <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
   <tx:method name="open*" propagation="REQUIRED" />
   <tx:method name="add*" propagation="REQUIRED" />
   <tx:method name="delete*" propagation="REQUIRED" />
   <tx:method name="upgrade*" propagation="REQUIRED" />
   <tx:method name="*" propagation="REQUIRED"/>
  </tx:attributes>
 </tx:advice>
</beans>
这种配置在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的基础上才能打,那结果是两个包都打不成安定北京,除非用源代码才能打得出这样的包.安定北京

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。