我正在尝试用@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 {
}
在interceptor中,我需要获取注释和赋值给reason属性的值。
发布于 2015-12-14 18:44:20
拦截器类来获取在类级别标记的注释值
@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();
}
}
https://stackoverflow.com/questions/34264713
复制相似问题