我正在尝试通过 Spring Aop AspectJ 样式获取注释的值,其中注释可以在类或方法上。我尝试了很多不同的东西,但只有当注释在方法上时我才能让它工作。我真的很想在类上注释 ONCE - 但建议该类的所有方法 - 并在建议中访问类注释的值。这是我结束的地方:
注解:
@Inherited
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "";
}
Run Code Online (Sandbox Code Playgroud)
方面:
@Aspect
public class MyAspect {
@Pointcut("execution(@com.myco.MyAnnotation * com.myco.somepackage..*.*(..))")
public void atExecution() { }
@Before("atExecution() && @annotation(myAnnotation)")
public void myAdvice(JoinPoint joinPoint, MyAnnotation myAnnotation) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?谢谢。