Search code examples
javaaspectjspring-aoppointcut

Pointcut confusion with inheritance


I am confused by writing a pointcut that matches all executions of a method. I tried the pointcut that should match all method-executions of class Alpha:

execution(* Alpha.*(..))

with the following class-hierachy

public class Alpha {
    public void alphaMethod() {...}
}
public class Beta extends Alpha {
    public void betaMethod() {
        alphaMethod();
    }
}

If the Main-program calls alphaMethod on an Beta-instance my advice is called like expected but the Main-program calls betaMethod that also calls alphaMethod inside my advice is not called and I don't understand why.

Aspect-Definition:

@Aspect
public class MyAspect {
    @Before(value = "execution(* Alpha.*(..))", argNames="joinPoint")
    public void myAdvice(JoinPoint joinPoint) {
        System.out.println("BEFORE: " + joinPoint.getSignature());
    }
}

Main-method:

Beta beta = ...;
beta.alphaMethod(); //advice is called
beta.betaMethod(); //advice is NOT called.

Solution

  • This is expected.

    Spring AOP uses proxy classes to wrap advised beans. When you call alphaMethod() from within a Beta method, the proxy isn't even aware of it.

    See this answer for more information.