【问题标题】:Aspect on super interface method implemented in abstract super class抽象超类中实现的超接口方法方面
【发布时间】:2015-01-23 09:37:59
【问题描述】:

我有一个与How to create an aspect on an Interface Method that extends from A "Super" Interface 非常相似的问题,但我的保存方法在一个抽象超类中。

结构如下-

接口:

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 中触发。 我的周围切入点缺少什么?

【问题讨论】:

    标签: java aspectj spring-aop pointcut


    【解决方案1】:

    我只想在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();
    }
    

    【讨论】:

      【解决方案2】:

      来自spring docs 示例:

      AccountService接口定义的任何方法的执行:
      execution(* com.xyz.service.AccountService.*(..))

      在你的情况下,它应该如下工作:

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

      【讨论】:

      • 我只想在ServiceInterface 上做切入点,如果我在SuperServiceInterface 上做这件事,它不会也拦截同样从SuperServiceInterface 继承的接口上的保存调用吗?
      • 试试execution(* com.xyz.service.ServiceInterface.save(..))?
      • 这并没有被拦截,可能是因为保存在界面中。我也试过execution(* com.xyz.service.ServiceInterface+.save(..)),但仍然没有被触发。如果接口指定save,服务实现save,这个切入点有效。
      • 我没有测试过!我将在虚拟设置中尝试一下。
      猜你喜欢
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 2019-01-24
      • 2020-02-04
      • 2011-11-20
      • 1970-01-01
      • 2011-03-19
      • 2012-03-14
      相关资源
      最近更新 更多