Search code examples
javaspringspring-aoppointcut

How to specify single pointcut for all classes that extend a specific class


I have multiple classes from different packages that extends a class Super. And i want to create an AOP pointcut around that match all the methods in all classes that extends Super. I have tried this:

@Around("within(com.mypackage.that.contains.super..*)")
public void aroundAllEndPoints(ProceedingJoinPoint joinPoint) throws Throwable {
        LOGGER.info("before Proceed ");
        joinPoint.proceed();
        LOGGER.info("after Proceed");
}

But it doesn't work. Any Suggestions?


Solution

  • The pointcut should be:

    within(com.mypackage.Super+)
    

    where com.mypackage.Super is the fully qualified base class name and + means "all subclasses". This works for Spring AOP. In AspectJ this would match too many joinpoints, not just method executions. Here is another pointcut that works for both Spring AOP and AspectJ:

    execution(* com.mypackage.Super+.*(..))