当前位置: 首页 >Java技术 > SpringBoot2.x 全局事务配置

SpringBoot2.x 全局事务配置

首先依旧还是在启动类上加上事务开启注解@EnableTransactionManagement
如下

@SpringBootApplication(scanBasePackages = "com.xxx")@MapperScan("com.xxx.service.db.repository")@EnableDiscoveryClient@EnableFeignClients@EnableTransactionManagementpublic class CSServiceApplication {	public static void main(String[] args) {		SpringApplication.run(MthCmsServiceApplication.class, args);	}}

AOP依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>  </dependency> 

全局事务管理配置如下,复制粘贴即用,但是得注意一点,增强的方法命名必须服从下面配置的规范

package com.xxx.service.config;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.TransactionDefinition;import org.springframework.transaction.TransactionManager;import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;import org.springframework.transaction.interceptor.RollbackRuleAttribute;import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;import org.springframework.transaction.interceptor.TransactionInterceptor;import java.util.Collections;/** * @description 全局事务管理 */@Aspect@Configurationpublic class CSTransactionConfig {private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.xxx.service.web.service.*.*(..))";private static final String[] REQUIRED_RULE_TRANSACTION = {"insert*", "create*", "add*", "save*","modify*", "update*", "del*", "delete*"};private static final String[] READ_RULE_TRANSACTION = {"select*", "get*", "query*", "search*", "count*","detail*", "find*"};@Autowiredprivate TransactionManager transactionManager;@Beanpublic TransactionInterceptor txAdvice() {RuleBasedTransactionAttribute REQUIRED = new RuleBasedTransactionAttribute();REQUIRED.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));REQUIRED.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);RuleBasedTransactionAttribute READONLY = new RuleBasedTransactionAttribute();READONLY.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));READONLY.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);READONLY.setReadOnly(true);NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();for (String s : REQUIRED_RULE_TRANSACTION) {source.addTransactionalMethod(s, REQUIRED);}for (String s : READ_RULE_TRANSACTION) {source.addTransactionalMethod(s, READONLY);}retu new TransactionInterceptor(transactionManager, source);}@Beanpublic Advisor txAdviceAdvisor(TransactionInterceptor txAdvice) {AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();pointcut.setExpression(AOP_POINTCUT_EXPRESSION);retu new DefaultPointcutAdvisor(pointcut, txAdvice);}}

总结
1、首先在使用事务之前,要在启动类里面引入注解 @EnableTransactionManagement
2、使用全局事务的时候,方法命名一定要在上述规范中,切勿出现奇葩命名

作者:懒虫虫~
来源链接:https://blog.csdn.net/jike11231/article/details/122940245

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/java/67964.html

分享给朋友:

“SpringBoot2.x 全局事务配置” 的相关文章