Search code examples
javaaspectjspring-aoppointcut

Aspect on super interface method implemented in abstract super class


I have a problem quite similar to: How to create an aspect on an Interface Method that extends from A "Super" Interface, but my save method is in an abstract super class.

The structure is as follows -

Interfaces:

public interface SuperServiceInterface {
    ReturnObj save(ParamObj);
}

public interface ServiceInterface extends SuperServiceInterface {
    ...
}

Implementations:

public abstract class SuperServiceImpl implements SuperServiceInterface {
    public ReturnObj save(ParamObj) {
        ...
    }
}

public class ServiceImpl implements ServiceInterface extends SuperServiceImpl {
    ...
}

I want to inspect any calls made to the ServiceInterface.save method.

The pointcut I have currently looks like this:

@Around("within(com.xyz.api.ServiceInterface+) && execution(* save(..))")
public Object pointCut(final ProceedingJoinPoint call) throws Throwable {
}

It gets triggered when the save method is put into the ServiceImpl, but not when it is in the SuperServiceImpl. What am I missing in my around pointcut?


Solution

  • I just want to pointcut on the ServiceInterface, if I do it on SuperServiceInterface wouldn't it also intercept save calls on interfaces that also inherit from SuperServiceInterface?

    Yes, but you can avoid that by restricting the target() type to ServiceInterface as follows:

    @Around("execution(* save(..)) && target(serviceInterface)")
    public Object pointCut(ProceedingJoinPoint thisJoinPoint, ServiceInterface serviceInterface)
        throws Throwable
    {
        System.out.println(thisJoinPoint);
        return thisJoinPoint.proceed();
    }