springboot2.0配置全局事务支持 附execution表达式理解
1.引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2.编写java配置
package cn.rivamed.framework.common.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.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.interceptor.DefaultTransactionAttribute; import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource; import org.springframework.transaction.interceptor.TransactionInterceptor; @Aspect @Configuration @EnableTransactionManagement public class TransactionConfig { private static final String ASPECTJ_EXPRESSION = "execution(* cn.rivamed..service..*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("remove*", txAttr_REQUIRED); source.addTransactionalMethod("update*", 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(ASPECTJ_EXPRESSION); return new DefaultPointcutAdvisor(pointcut, txAdvice()); } }
3.切面目录结构
4.execution理解
"execution(* cn.rivamed..service..*Service*.*(..))"
execution(): 表达式主体。
第一个*号:表示返回类型, *号表示所有的类型。
包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包
第二个*号:表示类名,*Service*表示包下包含Service的所有类。
*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数
5.@Transactional注解
@Transactional(rollbackOn = Exception.class) 和 @Transactional(rollbackFor = Exception.class)含义相同
⎛⎝官萧何⎠⎞一只快乐的爪哇程序猿;邮箱:1570608034@qq.com
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现