【发布时间】:2018-06-05 16:50:14
【问题描述】:
我正在尝试在 Spring RestController 中查找使用给定注释进行注释的方法。为了查看该 RestController 的方法上存在哪些注释,我执行了以下操作:
Map<String, Object> beans = appContext.getBeansWithAnnotation(RestController.class);
for (Map.Entry<String, Object> entry : beans.entrySet()) {
Method[] allMethods = entry.getValue().getClass().getDeclaredMethods();
for(Method method : allMethods) {
LOG.debug("Method: " + method.getName());
Annotation[] annotations = method.getDeclaredAnnotations();
for(Annotation annotation : annotations) {
LOG.debug("Annotation: " + annotation);
}
}
}
问题是我根本看不到任何注释,尽管事实上我知道我至少有一个带有@Retention(RetentionPolicy.RUNTIME) 注释的注释。有任何想法吗? CGLIB 是这里的一个因素吗? (作为控制器,所讨论的方法正在使用 CGBLIB 进行代理)。
【问题讨论】:
-
请同时添加注解接口代码
-
有问题的注解是Spring的@PreAuthorize注解。
-
使用 Spring
AnnotationUtils或至少AopUtils获取实际类。您获得了一个代理,并且由于注释不是继承的,您将看不到它们。还有你为什么需要这个? -
谢谢@M.Deinum,太好了。该要求涉及记录授权。如果您提交答案,我会将其标记为正确。
标签: java spring spring-mvc reflection cglib