0

我有这个切入点

@Pointcut("execution(@com.foo.bar.aspect.annotation.MyAnnotation* * (..))"
          + "&& @annotation(annot)")
public void anyFoo(MyAnnotation annot)
{

}

MyAnnotation看起来像这样:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
   boolean isFoo();

   String name;
}

假设我用这个注释注释了一个方法,其中 isFoo 设置为 true

@MyAnnotation(isFoo = true, name = "hello")
public void doThis()
{
   System.out.println("Hello, World");
}

如何编写我的切入点以使其仅匹配用MyAnnotaionAND注释的方法isFoo = true

我试过这个,但它似乎不起作用

@Pointcut("execution(@com.foo.bar.aspect.annotation.MyAnnotation(isFoo = true, *) * * (..))"
          + "&& @annotation(annot)")
public void anyFoo(MyAnnotation annot)
{

}
4

1 回答 1

2

你不能写这样的切入点,因为 AspectJ 不支持它。你需要使用类似的东西

@Pointcut("execution(@com.foo.bar.aspect.annotation.MyAnnotation* * (..))"
          + "&& @annotation(annot)")
public void anyFoo(MyAnnotation annot) {
    if (!annot.isFoo())
        return;
    // Only continue here if the annotation has the right parameter value
    // ...
}
于 2014-05-17T08:45:04.110 回答