在抽象 super class 中实现的 super 接口方法的方面

Aspect on super interface method implemented in abstract super class

我遇到的问题非常类似于:How to create an aspect on an Interface Method that extends from A "Super" Interface,但我的保存方法是在一个抽象的超级 class.

结构如下-

接口:

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 {
}

当 save 方法被放入 ServiceImpl 时会被触发,但当它被放入 SuperServiceImpl 时不会被触发。 我的 around 切入点缺少什么?

来自 spring docs 个示例:

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

在您的情况下,它应该按如下方式工作:

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

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?

是的,但您可以通过将 target() 类型限制为 ServiceInterface 来避免这种情况,如下所示:

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