如何在 Spring 代理中的私有方法上创建 @Trasactional

How to create @Trasactional on Private method in Spring Proxy

这是代码片段。

public class A{

  public void m1(){
    //Do some stuff
    m2();
  }

 @Transactional
 private m2(){
   // Some DB operations
 }
}

在上面的代码中,@Transactional 不起作用。

有没有一种方法可以让我仅在私有方法(而非 public)上创建 @Transactional

有人可以帮忙吗。

@Transactional 保留在私有方法上没有任何意义,因为此方法最终会在 class 本身中调用。所以代理永远不会应用于该方法。

When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings.