我有一个非常相似的问题:如何在从"超级"接口扩展的接口方法上创建一个方面,但我的保存方法在一个抽象的超类中。
结构如下——
接口:
public interface SuperServiceInterface {
ReturnObj save(ParamObj);
}
public interface ServiceInterface extends SuperServiceInterface {
...
}
实现:
public abstract class SuperServiceImpl implements SuperServiceInterface {
public ReturnObj save(ParamObj) {
...
}
}
public class ServiceImpl implements ServiceInterface extends SuperServiceImpl {
...
}
我想检查对ServiceInterface.save
方法进行的任何调用。
我目前的切入点如下所示:
@Around("within(com.xyz.api.ServiceInterface+) && execution(* save(..))")
public Object pointCut(final ProceedingJoinPoint call) throws Throwable {
}
当保存方法放入ServiceImpl
时,它会触发,但当它在SuperServiceImpl
中时不会触发。我在周围切入点中缺少什么?
我只想在
ServiceInterface
上切入点,如果我在SuperServiceInterface
上这样做,它是否也会拦截也继承自SuperServiceInterface
的接口上的保存调用?
是的,但您可以通过将target()
类型限制为ServiceInterface
来避免这种情况,如下所示:
@Around("execution(* save(..)) && target(serviceInterface)")
public Object pointCut(ProceedingJoinPoint thisJoinPoint, ServiceInterface serviceInterface)
throws Throwable
{
System.out.println(thisJoinPoint);
return thisJoinPoint.proceed();
}
来自春季文档示例:
执行由
AccountService
接口定义的任何方法:
execution(* com.xyz.service.AccountService.*(..))
在您的情况下,它应该按如下方式工作:
execution(* com.xyz.service.SuperServiceInterface.save(..))