Spring Boot AOP事务管理

代码实现

1import java.util.Collections; 2import java.util.HashMap; 3import java.util.Map; 4 5import org.aspectj.lang.annotation.Aspect; 6import org.springframework.aop.Advisor; 7import org.springframework.aop.aspectj.AspectJExpressionPointcut; 8import org.springframework.aop.support.DefaultPointcutAdvisor; 9import org.springframework.beans.factory.annotation.Autowired; 10import org.springframework.context.annotation.Bean; 11import org.springframework.context.annotation.Configuration; 12import org.springframework.transaction.PlatformTransactionManager; 13import org.springframework.transaction.TransactionDefinition; 14import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource; 15import org.springframework.transaction.interceptor.RollbackRuleAttribute; 16import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute; 17import org.springframework.transaction.interceptor.TransactionAttribute; 18import org.springframework.transaction.interceptor.TransactionInterceptor; 19 20/** 21 * @Title: TxAdviceInterceptor.java 22 * @Package com.cloud.aop 23 * @Description: 24 * @author ybwei 25 * @date 2018年9月3日 上午10:37:31 26 * @version V1.0 27 */ 28@Aspect 29@Configuration 30public class TxAdviceInterceptor { 31 private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.cloud.serviceImpl.*.*(..))"; 32 @Autowired 33 private PlatformTransactionManager transactionManager; 34 35 @Bean 36 public TransactionInterceptor txAdvice() { 37 NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource(); 38 /* 当前没有事务:只有select,非事务执行;有update,insert,delete操作,自动提交; 39 * 当前有事务:如果有update,insert,delete操作,支持当前事务 */ 40 RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute(); 41 readOnlyTx.setReadOnly(true); 42 readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); 43 /* 当前存在事务就使用当前事务,当前不存在事务就创建一个新的事务 */ 44 RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute(); 45 requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class))); 46 requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); 47 // requiredTx.setTimeout(TX_METHOD_TIMEOUT); 48 Map<String, TransactionAttribute> txMap = new HashMap<>(); 49 txMap.put("add*", requiredTx); 50 txMap.put("save*", requiredTx); 51 txMap.put("insert*", requiredTx); 52 txMap.put("update*", requiredTx); 53 txMap.put("delete*", requiredTx); 54 txMap.put("get*", readOnlyTx); 55 txMap.put("query*", readOnlyTx); 56 source.setNameMap(txMap); 57 TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source); 58 return txAdvice; 59 } 60 61 @Bean 62 public Advisor txAdviceAdvisor(TransactionInterceptor txAdvice) { 63 AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); 64 pointcut.setExpression(AOP_POINTCUT_EXPRESSION); 65 return new DefaultPointcutAdvisor(pointcut, txAdvice); 66 } 67}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Android So动态加载 优雅实现与原理分析

背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我

Spring方式初始化方法

第一种:实现ApplicationListener<ContextRefreshedEvent接口packagebdc.base;importjava.util.HashMap;importjava.util.List;importjava.util.Map;impor