gpt4 book ai didi

java - PointCut 将使用某些参数注释的方法与注释相匹配

转载 作者:行者123 更新时间:2023-12-02 05:49:28 25 4
gpt4 key购买 nike

我有这个切入点

@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");
}

如何编写切入点,使其仅与使用 MyAnnotaion AND isFoo = true 注释的方法匹配?

我试过了,但似乎不起作用

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

}

最佳答案

你不能写这样的切入点,因为 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
// ...
}

关于java - PointCut 将使用某些参数注释的方法与注释相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23702673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com