我正在尝试使用@Aspect 实现一个拦截器。我需要获取类级别的注释
这是我的拦截器
@Aspect
public class MyInterceptor {
@Around("execution(* com.test.example..*(..))")
public Object intercept(ProceedingJoinPoint pjp) throws Throwable {
Object result;
try {
result = pjp.proceed();
} catch (Throwable e) {
throw e;
}
return result;
}
}
这是我的注释
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String reason();
}
这是类
@MyAnnotation(reason="yes")
public class SomeClassImpl implements SomeClass {
}
在拦截器中,我需要获取注释和分配给 reason 属性的值。
最佳答案
拦截器类以获取在类级别标记的注释的值
@Aspect
@Component
public class MyInterceptor {
@Around("@target(annotation)")
public Object intercept(ProceedingJoinPoint joinPoint, MyAnnotation annotation) throws Throwable {
System.out.println(" called with '" + annotation.reason() + "'");
return joinPoint.proceed();
}
}
关于java - 如何从 ProceedingJoinPoint 获取类级别注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34264713/