2

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?

Community
  • 1
  • 1
adben002
  • 23
  • 1
  • 4

2 Answers2

2

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();
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
0

From the spring docs examples:

the execution of any method defined by the AccountService interface:
execution(* com.xyz.service.AccountService.*(..))

In your case it should work as follows:

execution(* com.xyz.service.SuperServiceInterface.save(..))

Dev Naruka
  • 193
  • 1
  • 7
  • 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`? – adben002 Jan 23 '15 at 11:48
  • Try `execution(* com.xyz.service.ServiceInterface.save(..))`? – Dev Naruka Jan 23 '15 at 11:53
  • This does not get intercepted, possibly because the save is in the interface. I tried `execution(* com.xyz.service.ServiceInterface+.save(..))` aswell, but still was not triggered. If the interface specifies save and the service implements save, this pointcut works. – adben002 Jan 23 '15 at 12:28
  • I didn't tested it! I will try it in a dummy setup. – Dev Naruka Jan 23 '15 at 12:41