[英]AspectJ - Multiple @annotation Pointcut
我不能用“||”做一个切入点运算符和多个注释。 我正在尝试为一些 JBehave 注释(@Given、@Then、@When)创建一个切入点。
这工作正常:
@Pointcut("@annotation(given)")
public void jBehaveGivenPointcut(Given given) { }
如果我创建它并提出建议,它也有效。
为三个注释制作切入点的语法是什么? 由于我在其他切入点中使用了逻辑 OR 运算符,因此我认为它类似于:
@Pointcut("@annotation(given) || @annotation(then) || @annotation(when) ")
public void jBehaveGivenPointcut(Given given, Then then, When when) { }
但它不起作用,我得到一个不一致的绑定异常。 我尝试了其他组合,但找不到可以解决问题的组合。
你想要的不能这样做,因为,正如错误消息所说,注释绑定是不一致的:你不能同时绑定所有三个注释,因为它们在(可能是互斥的)切入点的 OR 部分,即通常只有可以绑定其中的一个(除非您为同一个方法分配多个注释)。 您的期望可能是 AspectJ 可以通过将null
分配给其他两个(如果绑定了)来处理这种不一致,但这不是编译器现在的工作方式。
我可以提供一种解决方法,它涉及反射而不是使用@annotation()
绑定。
驱动程序应用:
package de.scrum_master.app;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
public class Application {
public static void main(String[] args) {
doGiven("foo");
doSomething("bar");
doWhen(11);
doSomethingElse(22);
doThen();
}
@Given("an input value") public static void doGiven(String string) {}
@When("I do something") public static void doWhen(int i) {}
@Then("I should obtain a result") public static boolean doThen() { return true; }
public static void doSomething(String string) {}
public static void doSomethingElse(int i) {}
}
方面:
package de.scrum_master.aspect;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
public class JBehaveInterceptor {
@Pointcut("execution(@org.jbehave.core.annotations.* * *(..))")
public void jBehavePointcut() {}
@Before("jBehavePointcut()")
public void jBehaveAdvice(JoinPoint.StaticPart thisJoinPointStaticPart) {
Method method = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod();
for (Annotation jBehaveAnnotation : method.getAnnotations()) {
if (jBehaveAnnotation.annotationType().getPackage().getName().equals("org.jbehave.core.annotations"))
System.out.println(thisJoinPointStaticPart + " -> " + jBehaveAnnotation);
}
}
}
如您所见,切入点仅拦截由org.jbehave.core.annotations.*
注释的方法,这大大缩小了切入点匹配的范围 - 不仅仅是@Given
、 @When
、 @Then
,但也许这甚至是您想要的,因为JBehave 提供了更多的注释。
在建议中,我们遍历所有方法注释,因为可能不仅仅是 JBehave 注释。 如果任何注释包名称与相应的 JBehave 包匹配,我们就会做一些事情(在这种情况下将注释打印到标准输出)。
我希望这能解决你的问题。 我无法为您扩展 AspectJ 语言,这是我能想到的最好的语言。 无论如何,这会产生以下输出:
execution(void de.scrum_master.app.Application.doGiven(String)) -> @org.jbehave.core.annotations.Given(priority=0, value=an input value)
execution(void de.scrum_master.app.Application.doWhen(int)) -> @org.jbehave.core.annotations.When(priority=0, value=I do something)
execution(boolean de.scrum_master.app.Application.doThen()) -> @org.jbehave.core.annotations.Then(priority=0, value=I should obtain a result)
这有效:
@Component
@Aspect
@RequiredArgsConstructor
public class PermissionAspect {
@Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
public void getMapping(){}
@Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
public void postMapping(){}
@Pointcut("@annotation(org.springframework.web.bind.annotation.PutMapping)")
public void putMapping(){}
@Pointcut("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
public void deleteMapping(){}
@Around("(execution(* xx.*.controller.*.*(..))) && (getMapping() || postMapping() || putMapping() || deleteMapping())")
Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
一个简单的解决方案是创建多个切入点并转发到包含处理程序逻辑的通用方法:
@Pointcut("@annotation(first)")
public void onFirst(First first) {
Common common = getCommon(first);
handle(common);
}
@Pointcut("@annotation(second)")
public void onSecond(Second second) {
Common common = getCommon(second);
handle(common);
}
private void handle(Common common){...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.