Does Spring @Transactional attribute work on a private method?

2022-08-31 05:35:57

If I have a @Transactional -annotation on a private method in a Spring bean, does the annotation have any effect?

If the annotation is on a public method, it works and open a transaction.@Transactional

public class Bean {
  public void doStuff() {
     doPrivateStuff();
  }
  @Transactional
  private void doPrivateStuff() {

  }
}

...

Bean bean = (Bean)appContext.getBean("bean");
bean.doStuff();

答案 1

The answer your question is no - will have no effect if used to annotate private methods. The proxy generator will ignore them.@Transactional

This is documented in Spring Manual chapter 10.5.6:

Method visibility and @Transactional

When using proxies, you should apply the annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.@Transactional@Transactional


答案 2

The Question is not private or public, the question is: How is it invoked and which AOP implementation you use!

If you use (default) Spring Proxy AOP, then all AOP functionality provided by Spring (like ) will only be taken into account if the call goes through the proxy. -- This is normally the case if the annotated method is invoked from another bean.@Transactional

This has two implications:

  • Because private methods must not be invoked from another bean (the exception is reflection), their Annotation is not taken into account.@Transactional
  • If the method is public, but it is invoked from the same bean, it will not be taken into account either (this statement is only correct if (default) Spring Proxy AOP is used).

@See Spring Reference: Chapter 9.6 9.6 Proxying mechanisms

IMHO you should use the aspectJ mode, instead of the Spring Proxies, that will overcome the problem. And the AspectJ Transactional Aspects are woven even into private methods (checked for Spring 3.0).


推荐