Vitalii
Vitalii

Reputation: 11071

Spring AOP pointcut for all public methods of an annotatted class (including parent class methods)

I have two classes

public class ParentTestClass {
    public void publicMethodOfParent() {
    }
}

@Component
@MyAnnotation
public class ChildTestClass extends ParentTestClass {
    public void publicMethodOfChild() {
    }
}

With Spring AOP I need to wrap:

Here is my pointcut


@Around("(@within(MyAnnotation) && execution(public * *(..))) || @annotation(MyAnnotation)")
public Object myWrapper(ProceedingJoinPoint invocation) throws Throwable {
   // ...
}

This works for public methods of ChildTestClass but ParentTestClass#publicMethodOfParent is not wrapped when I make a call childTestClass.publicMethodOfParent() How can I include parent methods?

Upvotes: 0

Views: 1567

Answers (1)

R.G
R.G

Reputation: 7131

Following pointcut expression will intercept the parent methods as well

From the documentation

@Pointcut("within(com.app..*) && execution(public * com.app..*.*(..))")
public void publicMethodsInApp() {
}

@Around("(publicMethodsInApp() && @target(MyAnnotation)) || "
        + "(publicMethodsInApp() && @annotation(MyAnnotation))")
public Object myWrapper(ProceedingJoinPoint invocation) throws Throwable {
 //..
}

@target: Limits matching to join points (the execution of methods when using Spring AOP) where the class of the executing object has an annotation of the given type.

Upvotes: 2

Related Questions