spring 事物配置几种

第一种  tx aop 空间

引入空间

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    ">

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
 </bean>
 <!-- 定义事务管理的通知 -->
 <tx:advice id="txadvice" transaction-manager="transactionManager">
  <!-- 定义业务方法的事物管理特性 -->
  <tx:attributes>
   <tx:method name="qry*" propagation="REQUIRED" read-only="true"></tx:method>
   <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"></tx:method>
  </tx:attributes>
 </tx:advice>

 <!-- 定义AOP配置:切点 -->
 <aop:config>
  <aop:pointcut id="tranaop"
   expression="execution(* com.zte.adc.report.service.*.*(..))" />
  <aop:advisor advice-ref="txadvice" pointcut-ref="tranaop" />
 </aop:config>

第二种 通过xml 标记 拦截器来实现

    <bean id="transactionManager"

              class="org.springframework.transaction.jta.JtaTransactionManager">

 

</bean>

 

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

       <!-- 事务拦截器bean需要依赖注入一个事务管理器 -->

    <property name="transactionManager" ref="transactionManager"/>

       <property name="transactionAttributes">

              <!-- 下面定义事务传播属性-->

              <props>

                     <prop key="insert*">PROPAGATION_REQUIRED</prop>

                     <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>

                     <prop key="*">PROPAGATION_REQUIRED</prop>

              </props>

       </property>

       </bean>

 

   <!-- 定义BeanNameAutoProxyCreator 该类 用于说明那些类 能够被拦截-->

    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

       <!-- 指定对满足哪些bean name的bean自动生成业务代理 -->

           <property name="beanNames">

            <!-- 下面是所有需要自动创建事务代理的bean-->

            <list>

                 <value>test1</value>

                <value>test2</value>

            </list>

            <!-- 此处可增加其他需要自动创建事务代理的bean-->

           </property>

           (2)<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->

           <property name="beanNames">

                <value>*DAO,*.Service</value>

           </property>

        <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->

        <property name="interceptorNames">

            <list>

                <value>transactionInterceptor</value>

                    <!-- 此处可增加其他新的Interceptor -->

             </list>

        </property>

</bean>

原文地址:https://www.cnblogs.com/liaomin416100569/p/9331869.html