SpringBoot 类中方法调用方法事务失效问题


@Sevice
public class Test {

  public void a(){
    b();
  }

  @Transactional
  public void b(){
    System.out.print("b");
  }
}

此时事务时不生效的,因为@Transactional注解事务是通过代理来控制的,方法调用本类方法,事务不会生效。

 

解决方法:

// 增加该注解开启代理
@EnableAspectJAutoProxy(proxyTargetClass = true,exposeProxy = true)
@Sevice
public class Test {

  public void a(){
    // 通过代理方式调用方法
    ((Test)AopContext.currentProxy()).b();
    // b();
  }

  @Transactional
  public void b(){
    System.out.print("b");
  }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM