Spring AOP not working, when the method is called internally within a bean

Thank you jst for clearing the things up. Just for the information purposes for the future developer in SO, I’m posting the full answer to this question


Lets assume that there is a bean from SimplePojo

public class SimplePojo implements Pojo {
    public void foo() {
        this.bar();
    }
    public void bar() {
        ...
    }
}

When we call the method foo(), it reinvokes the method bar() inside it. Even thought the method foo() is invoked from the AOP Proxy, the internal invocation of the bar() is not covered by the AOP Proxy.

Proxy calls

So eventually this makes, if there are any advices attached to the method bar() to not get invoked

Solution

Use AopContext.currentProxy() to call the method. Unfortunately this couples the logic with AOP.

public void foo() {
   ((Pojo) AopContext.currentProxy()).bar();
}

Reference:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies

Leave a Comment