问题
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?
回答1:
I just want to pointcut on the
ServiceInterface
, if I do it onSuperServiceInterface
wouldn't it also intercept save calls on interfaces that also inherit fromSuperServiceInterface
?
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();
}
回答2:
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(..))
来源:https://stackoverflow.com/questions/28106880/aspect-on-super-interface-method-implemented-in-abstract-super-class