弹簧方面切入点定义

时间:2014-07-13 11:49:51

标签: spring aspectj spring-aop aspect

在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");

        }

    }

这可行吗?

由于

住。

1 个答案:

答案 0 :(得分:0)

这是你可以用代码风格做的事情:

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()模型制作该语法并不是那么快。

相关问题