Why doesn't Spring's @Transactional work on protected methods?

前端 未结 3 947
醉梦人生
醉梦人生 2020-12-17 01:48

From Does Spring @Transactional attribute work on a private method?

When using proxies, you should apply the @Transactional annotation only to metho

3条回答
  •  醉酒成梦
    2020-12-17 02:35

    Because of this:

    In proxy mode (which is the default), only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!

    And this:

    Due to the proxy-based nature of Spring's AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn't applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!

    The Spring guys would probably want to keep consistency with JDK proxies. You wouldn't want to have different proxy configuration and different results based on JDK versus CGLIB.

提交回复
热议问题