AspectJ 解决Spring @Transactional 坑点 步骤记录

上一篇文章提到Spring @Transactional 坑点中的前两个坑点都是用比较常规的方法解决的,spring官方提到过aspectj这种解决方案,随后我花时间研究了一下确实解决了,大概步骤是这样的:
1、pom.xml中加maven依赖:

<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>1.9.4</version>
</dependency>



<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>aspectj-maven-plugin</artifactId>
   <version>1.8</version>
   <configuration>
       <complianceLevel>1.8</complianceLevel>
       <source>1.8</source>
       <target>1.8</target>
       <aspectLibraries>
           <aspectLibrary>
               <groupId>org.springframework</groupId>
               <artifactId>spring-aspects</artifactId>
           </aspectLibrary>
       </aspectLibraries>
   </configuration>
   <executions>
       <execution>
           <goals>
               <goal>compile</goal>
           </goals>
       </execution>
   </executions>
</plugin>

2、IDEA安装AspectJ插件,确保这两个插件安装并启用(据说只有旗舰版才支持)
在这里插入图片描述
3、设置java compiler,需要点击 Apply,如下图:
在这里插入图片描述
4、配置类上配置@EnableTransactionManagement(mode = AdviceMode.ASPECTJ),以spring boot应用为例:
在这里插入图片描述
5、启动spring boot应用,查看效果:
在这里插入图片描述
从上图中可以看出,确实解决了事务不生效的问题,至于原理,我暂时也没有研究过,只能感叹字节码技术的强大之处。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值