SpringBoot 平台事务管理,可不用@Transaction 注解(统一规范方法名)方式还有其它

SpringBoot 平台事务管理,可不用@Transaction 注解(统一规范方法名)方式还有其它


  方式一

import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

    /**
      * 

     * 平台事务增强      *

     *      * @author user      */ @Aspect     @Configuration     public class TransactionalConfigForBoot {      private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.***.service..*.*(..))"; @Autowired private PlatformTransactionManager transactionManager; @Bean public TransactionInterceptor txAdvice() {         DefaultTransactionAttribute txAttr_REQUIRED = new DefaultTransactionAttribute();         txAttr_REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);         DefaultTransactionAttribute txAttr_REQUIRED_READONLY = new DefaultTransactionAttribute();         txAttr_REQUIRED_READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);         txAttr_REQUIRED_READONLY.setReadOnly(true);         NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();         source.addTransactionalMethod("save*", txAttr_REQUIRED);         source.addTransactionalMethod("add*", txAttr_REQUIRED);         source.addTransactionalMethod("create*", txAttr_REQUIRED);         source.addTransactionalMethod("update*", txAttr_REQUIRED);         source.addTransactionalMethod("delete*", txAttr_REQUIRED);         source.addTransactionalMethod("exec*", txAttr_REQUIRED);         source.addTransactionalMethod("set*", txAttr_REQUIRED);         source.addTransactionalMethod("get*", txAttr_REQUIRED_READONLY);         source.addTransactionalMethod("query*", txAttr_REQUIRED_READONLY);         source.addTransactionalMethod("find*", txAttr_REQUIRED_READONLY);         source.addTransactionalMethod("list*", txAttr_REQUIRED_READONLY);         source.addTransactionalMethod("count*", txAttr_REQUIRED_READONLY);         source.addTransactionalMethod("is*", txAttr_REQUIRED_READONLY);         return new TransactionInterceptor(transactionManager, source);     } @Bean public Advisor txAdviceAdvisor() {         AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();         pointcut.setExpression(AOP_POINTCUT_EXPRESSION);         return new DefaultPointcutAdvisor(pointcut, txAdvice());     } }

 



    方式二(推荐):(简单明了..不过目前遇到问题在多个service方法进行方法调用的时候,可能存在传播行为的问题,目前仍在排查中)

    package com.zhiche.opbaas.config;
    
    import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.interceptor.TransactionInterceptor;
    
    import java.util.Properties;
    
    @Configuration   //这里使用Component或Configuration事务都可以生效
    public class TransactionalConfigForBoot {
    
        private DataSourceTransactionManager transactionManager;
    
        @Autowired
        public void setTransactionManager(DataSourceTransactionManager transactionManager) {
            this.transactionManager = transactionManager;
        }
    
        // 创建事务通知
        @Bean(name = "txAdvice")
        public TransactionInterceptor getAdvisor() {
            Properties properties = new Properties();
            properties.setProperty("get*", "PROPAGATION_NOT_SUPPORTED,-Exception,readOnly");
            properties.setProperty("add*", "PROPAGATION_REQUIRED,-Exception");
            properties.setProperty("save*", "PROPAGATION_REQUIRED,-Exception");
            properties.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
            properties.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception");
            properties.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception");
            return new TransactionInterceptor(transactionManager, properties);
        }
    
        @Bean
        public BeanNameAutoProxyCreator txProxy() {
            BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
            creator.setInterceptorNames("txAdvice");
            creator.setBeanNames("*Service", "*ServiceImpl");
            creator.setProxyTargetClass(true);
            return creator;
        }
    }



    

你可能感兴趣的:(SpringBoot 平台事务管理,可不用@Transaction 注解(统一规范方法名)方式还有其它)