Same class invoke NOT effective in Spring AOP cglib

前端 未结 2 594
不知归路
不知归路 2020-12-19 15:21

Suppose we have following class

@Service
class MyClass {

    public void testA() { 
        testB();
     }

    @Transactional
    public void testB() { ..         


        
相关标签:
2条回答
  • 2020-12-19 15:22

    You almost have it right. The proxy looks something more like this:

    class Cglib$MyClass extends MyClass {
    
      MyClass delegate;
    
      @Override
      public void testB() {
        // ...do transactional things
        delegate.testB();
      }
    }
    

    Any call is forwarded by Spring which is why your nested annotations are not activated.

    Also, if a virtual method like testA was overridden, Spring could not avoid to invoke the overridden method.

    0 讨论(0)
  • 2020-12-19 15:35

    It is a well-known and documented (please search for the term "self-invocation") fact that Spring AOP, due to its proxy-based nature, does not and cannot capture internal method calls like this.someMethod(..).

    So as you said, you either need to explicitly refer to the exposed proxy object or alternatively switch from Spring AOP to full AspectJ via load-time weaving.

    0 讨论(0)
提交回复
热议问题