0

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:

  • all calls for all public methods that are annotated with @MyAnnotation if annotation is put on class level
  • all methods that are annotated with @MyAnnotation if annotation is on the method level.

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?

Vitalii
  • 10,091
  • 18
  • 83
  • 151

1 Answers1

2

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.

R.G
  • 6,436
  • 3
  • 19
  • 28
  • It will work but I need to have something generic without class names `within(ParentTestClass)` – Vitalii Apr 24 '20 at 13:28
  • Is the requirement to intercept as follows : based on the annotation and intercept the parent methods ? – R.G Apr 24 '20 at 13:29
  • I have interfaces outside the project defining some methods for Spring feign clients. Those interfaces are extended by interfaces inside the project and marked with annotation. Every call I need to perform an action. So the requirement is to wrap all calls of public methods of feign client beans. – Vitalii Apr 24 '20 at 13:35
  • I have updated the answer which does not involve reference to any class and only based on annotations. This works on the sample code you shared. If this is not meeting your requirement , could you please update the question with the exact code sample that needs to be intercepted ? – R.G Apr 24 '20 at 13:38
  • If I add just a target it throws an exception `java.lang.IllegalArgumentException: Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages` – Vitalii Apr 24 '20 at 14:07
  • The code I shared works for me with the example you provided. It would be easy if you can share an actual code that you want to advice. – R.G Apr 24 '20 at 14:09
  • 2
    Not sure of your usecase , but could you please try to narrow down the methods from your specific packages like `&& execution(public * com.path.to.your.package.*(..)))` . This is to exclude the framework specific classes from the matcher – R.G Apr 24 '20 at 14:28