【问题标题】:spring aspect pointcut definition弹簧方面切入点定义
【发布时间】:2014-07-13 11:49:51
【问题描述】:

在 Spring 中,我们可以共享常见的切入点定义,如下所示

@Aspect
public class SystemArchitecture {

  /**
   * A join point is in the web layer if the method is defined
   * in a type in the com.xyz.someapp.web package or any sub-package
   * under that.
   */
  @Pointcut("within(com.xyz.someapp.web..*)")
  public void inWebLayer() {}

}

上面可以像下面这样使用

@Aspect
public class MyAspect {


    @AfterThrowing(pointcut = "inWebLayer()  ")
    public void processError(JoinPoint jp) {
        logger.info("Enter:processError");

    }

}

是否可以将关节点传递给共享点切割定义。

如下所示,myCustomCheck 是另一个共享切入点定义,它根据传递给它的关节点检查某些内容。

 @Aspect
    public class MyAspect {


        @AfterThrowing(pointcut = "inWebLayer() && myCustomCheck(jp) ")
        public void processError(JoinPoint jp) {
            logger.info("Enter:processError");

        }

    }

这可行吗?

谢谢

生活。

【问题讨论】:

    标签: spring aspectj spring-aop aspect


    【解决方案1】:

    这是您可以在代码样式中执行的操作:

    public static boolean checkLine(JoinPoint tjp, int l) {
        return tjp.getSourceLocation().getLine()==l;
    }
    
    before(): execution(* just*(..)) && if(checkLine(thisJoinPoint,16)) {
        System.out.println("Execution!");
    }
    

    不完全将连接点传递给另一个切入点,而是将其传递给辅助方法。

    我认为在注释样式语法中也有类似的事情,但在使用它支持的 if() 模型来制作该语法方面并不是那么快。

    【讨论】:

    • thisJoinPoint 是什么?如果我们这样访问,我们可以获得对 joinpoint 的引用吗?
    • thisJoinPoint 是一个众所周知的变量,您可以在 JoinPoint 对象类型的代码样式切入点/建议中使用它。对于您正在使用的注释样式方面,您像在示例中所做的那样明确提及它: public void processError(JoinPoint jp) { 如果使用该语法,我只是不是 100% 将它和 if() .注释样式 if() 语法需要一些试验,这会略有不同。
    猜你喜欢
    • 2011-09-19
    • 1970-01-01
    • 2013-10-15
    • 2018-08-25
    • 2021-03-07
    • 2020-10-09
    • 2013-04-09
    • 1970-01-01
    • 2011-06-30
    相关资源
    最近更新 更多