spring 配置 事物创建代理 BeanNameAutoProxyCreator

<bean id="zwAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      <property name="interceptorNames">(事物拦截器)
      <list>
             <idref bean="zwInterceptor"/>

         </list>
      </property>
      <property name="beanNames">需要配置的bean列表
         <list>
             <idref bean="zwDao"/>

             ...

          </list>
      </property>
  </bean>

<bean id="zwInterceptor class="org.springframework.transaction.interceptor.TransactionInterceptor">

         <property name="transactionManager">
            <ref bean="transactionManager"/>(自己配置)
        </property>
        <property name="transactionAttributeSource">(管理TransactionAttribute)
            <ref local="listenerNameMatchBase"/>(设定事务的策略)
        </property>

</bean>

   
 <bean id="listenerNameMatchBase"
        class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
         <property name="properties">
            <props>
             <prop key="doAfter">PROPAGATION_REQUIRED</prop>(根据方法名字监控key方法这里试doafter,也可以是insert*等)
            </props>
         </property>
  </bean>

备注

事务传播行为种类

Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,

它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播:

事务传播行为类型

事务传播行为类型

说明

PROPAGATION_REQUIRED

如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。

PROPAGATION_SUPPORTS

支持当前事务,如果当前没有事务,就以非事务方式执行。

PROPAGATION_MANDATORY

使用当前的事务,如果当前没有事务,就抛出异常。

PROPAGATION_REQUIRES_NEW

新建事务,如果当前存在事务,把当前事务挂起。

PROPAGATION_NOT_SUPPORTED

以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

PROPAGATION_NEVER

以非事务方式执行,如果当前存在事务,则抛出异常。

PROPAGATION_NESTED

如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类 似的操作。

猜你喜欢

转载自zwxiaole.iteye.com/blog/1580047