在抽象超类中实现的超接口方法方面



我有一个非常相似的问题:如何在从"超级"接口扩展的接口方法上创建一个方面,但我的保存方法在一个抽象的超类中。

结构如下——

接口:

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(..))

最新更新