Springboot 注解声名事务编写&xml配置事务编写

1.  注解声名事务编写

/**

 * springboot 注解式声明事务,使用聲明式事務配置,可以有效規範代碼

 * @author Administrator

 */

@Configuration

public class ApplicationContextTransactional {

      //事务方法超时时间设置

      private static final int TX_METHOD_TIMEOUT=5;

      //AOP切面的切点表达式

      private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.baihoo.springboot.service.impl.*.*(..))";

      //注入事务管理器

      @Autowired

      private PlatformTransactionManager transactionManager;

      /**

       * 增强(事务)的属性的配置

       *       isolation:DEFAULT  :事务的隔离级别.

       *       propagation              :事务的传播行为.

       *       read-only                        :false.不是只读

       *       timeout                          :-1

       *       no-rollback-for         :发生哪些异常不回滚

       *       rollback-for                    :发生哪些异常回滚事务

       * @return

       */

      @Bean

      public TransactionInterceptor txAdvice() {

           /*增强(事务)的属性的配置

            * <tx:attributes>

            * */

           NameMatchTransactionAttributeSource txAttributeS = new NameMatchTransactionAttributeSource();

           /*propagation="REQUIRED" , timeout=5 ;rollback-for=".. , .."配置*/

           RuleBasedTransactionAttribute requiredAttr = new RuleBasedTransactionAttribute();

      requiredAttr.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

           requiredAttr.setTimeout(TX_METHOD_TIMEOUT);

           requiredAttr.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));

           /*propagation="SUPPORTS" , readOnly="true"配置*/

           RuleBasedTransactionAttribute supportsAttr = new RuleBasedTransactionAttribute();

      supportsAttr.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);

           supportsAttr.setReadOnly(true);

           /*

            注意:方法名称来自类匹配的到方法

                save*, “*”表示匹配任意個字符】

                <tx:method .../>

            */

           Map<String , TransactionAttribute>   txMethod = new HashMap<String , TransactionAttribute>();

           txMethod.put("save*", requiredAttr);

           txMethod.put("add*", requiredAttr);

           txMethod.put("insert*", requiredAttr);

           txMethod.put("update*", requiredAttr);

          

           txMethod.put("transfer*", requiredAttr); //service層的轉賬業務方法開啓事務增强通知

          

           txMethod.put("find*", supportsAttr);

           txMethod.put("select*", supportsAttr);

           txMethod.put("get*", supportsAttr);

           txAttributeS.setNameMap(txMethod);

           TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager , txAttributeS);

           return txAdvice;

      }

      /**

       * AOP配置定义切面和切点的信息

       * @return

       */

      @Bean

      public Advisor txAdviceAdvisor() {

           AspectJExpressionPointcut pointcut= new AspectJExpressionPointcut();

           pointcut.setExpression(AOP_POINTCUT_EXPRESSION);

           return new DefaultPointcutAdvisor(pointcut , txAdvice());

      }

}

2. xml配置事务编写

<?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:context="http://www.springframework.org/schema/context"

      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.xsd

      http://www.springframework.org/schema/context

      http://www.springframework.org/schema/context/spring-context.xsd

      http://www.springframework.org/schema/aop

      http://www.springframework.org/schema/aop/spring-aop.xsd

      http://www.springframework.org/schema/tx

      http://www.springframework.org/schema/tx/spring-tx.xsd">

      <!--

      使用聲明式事務配置,可以有效規範代碼

       -->

       <!--

       配置事務管理器

        -->

        <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

           <!-- 注入连接池的对象,通过连接池对象创建模板. -->

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

        </bean>

        <!--

        聲明式配置事務通知

         -->

        <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">

               <!-- 增强(事务)的属性的配置 -->

               <tx:attributes>

                     <!-- 注意:方法名称来自类匹配的到方法

                      save*, “*”表示匹配任意個字符】

                  -->

                     <tx:method name="save*" propagation="REQUIRED"/>

                     <tx:method name="add*" propagation="REQUIRED"/>

                     <tx:method name="insert*" propagation="REQUIRED"/>

                     <tx:method name="update*" propagation="REQUIRED"/>

                     <tx:method name="delete*" propagation="REQUIRED"/>

                     <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>

                     <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>

                     <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>

               </tx:attributes>

        </tx:advice>

        <!-- aop配置定义切面和切点的信息 -->

        <aop:config>

               <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.baihoo.ssm.service.impl.*.*(..))"/>

        </aop:config>

</beans>

猜你喜欢

转载自blog.csdn.net/baiHoo_chen/article/details/81324203
今日推荐