gpt4 book ai didi

java - Spring AOP : is there a way to make @target work for indirect annotations?

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

我有一个注释:

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface MyAnnotation {
}

我用它注释 Spring MVC Controller :

@MyAnnotation
public class TestController { ... }

然后我添加一条建议,其中包含以下内容:

@Pointcut("@target(MyAnnotation)")
public void annotatedWithMyAnnotation() {}

@Around("annotatedWithMyAnnotation()")
public Object executeController(ProceedingJoinPoint point) throws Throwable { ... }

Advice方法调用成功。

现在我有一堆共享相同注释的 Controller ,我想使用构造型注释对它们进行分组。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@MyAnnotation
... other annotations
public @interface StereotypeAnnotation {
}

然后我用 @StereotypeAnnotation 注释我的 Controller :

@StereotypeAnnotation
public class TestController { ... }

Controller 不再直接包含@MyAnnotation

问题是在这种情况下@target切入点停止匹配我的 Controller ,并且不建议使用它们。

有没有办法定义一个切入点来匹配具有此类间接注释的 Controller ?

最佳答案

我用纯AspectJ重新创建了这种情况,因为我不太喜欢Spring AOP。这就是为什么我在通知的切入点前面添加了一个额外的 execution(* *(..)) && 以避免匹配 Spring AOP 中不可用的其他连接点,例如 call() 。如果您愿意,可以在 Spring AOP 中删除它。

好吧,让我们按照您的描述来创建这种情况:

package de.scrum_master.app;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
public @interface MyAnnotation {}
package de.scrum_master.app;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@MyAnnotation
public @interface StereotypeAnnotation {}
package de.scrum_master.app;

@MyAnnotation
public class TestController {
public void doSomething() {
System.out.println("Doing something");
}
}
package de.scrum_master.app;

@StereotypeAnnotation
public class AnotherController {
public void doSomething() {
System.out.println("Doing yet another something");
}
}

这是我们的纯 Java 驱动程序应用程序(没有 Spring):

package de.scrum_master.app;

public class Application {
public static void main(String[] args) {
new TestController().doSomething();
new AnotherController().doSomething();
}
}

这就是方面:

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MetaAnnotationAspect {
@Pointcut(
"@target(de.scrum_master.app.MyAnnotation) || " +
"@target(de.scrum_master.app.StereotypeAnnotation)"
)
public void solutionA() {}

@Around("execution(* *(..)) && solutionA()")
public Object executeController(ProceedingJoinPoint point) throws Throwable {
System.out.println(point);
return point.proceed();
}
}

日志输出将是:

execution(void de.scrum_master.app.TestController.doSomething())
Doing something
execution(void de.scrum_master.app.AnotherController.doSomething())
Doing yet another something

到目前为止,一切都很好。但是如果我们添加另一层嵌套呢?

package de.scrum_master.app;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@StereotypeAnnotation
public @interface SubStereotypeAnnotation {}
package de.scrum_master.app;

@SubStereotypeAnnotation
public class YetAnotherController {
public void doSomething() {
System.out.println("Doing another something");
}
}
package de.scrum_master.app;

public class Application {
public static void main(String[] args) {
new TestController().doSomething();
new AnotherController().doSomething();
new YetAnotherController().doSomething();
}
}

那么切入点将不再与嵌套的元/原型(prototype)注释匹配:

execution(void de.scrum_master.app.TestController.doSomething())
Doing something
execution(void de.scrum_master.app.AnotherController.doSomething())
Doing yet another something
Doing another something

我们必须显式添加 || @target(de.scrum_master.app.StereotypeAnnotation) 到切入点,即我们必须知道层次结构中的所有注释类名称。有一种方法可以使用 within() 切入点指示符的特殊语法来克服这个问题,另请参阅 my other answer here :

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MetaAnnotationAspect {
@Pointcut(
"within(@de.scrum_master.app.MyAnnotation *) || " +
"within(@(@de.scrum_master.app.MyAnnotation *) *) || " +
"within(@(@(@de.scrum_master.app.MyAnnotation *) *) *)"
)
public void solutionB() {}

@Around("execution(* *(..)) && solutionB()")
public Object executeController(ProceedingJoinPoint point) throws Throwable {
System.out.println(point);
return point.proceed();
}
}

控制台日志更改为:

execution(void de.scrum_master.app.TestController.doSomething())
Doing something
execution(void de.scrum_master.app.AnotherController.doSomething())
Doing yet another something
execution(void de.scrum_master.app.YetAnotherController.doSomething())
Doing another something

看到了吗?我们只需要知道一个注释类,即MyAnnotation,就可以覆盖元注释的两层嵌套。添加更多级别将很简单。我承认这种注释嵌套看起来很做作,我只是想向您解释一下您有哪些选项。

关于java - Spring AOP : is there a way to make @target work for indirect annotations?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53723240/

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