【发布时间】:2012-02-16 23:17:10
【问题描述】:
在通过反射查找属于通过 CGLIB 代理的类的方法上的注释时,我遇到了一种奇怪的行为。我们在 Spring 中使用 CGLIB,如果我仅使用注释对方法进行注释,则效果很好(我可以通过对应的 Method 对象上的 getAnnotations() 方法检索注释)。如果我用 2 个注释来注释方法(无论注释的顺序如何),getAnnotations() 只返回null。两个注释都有RetentionPolicy.RUNTIME。
我读到 CGLIB 存在一些问题,但奇怪的是它只适用于一个注释,当我放置 2 个注释时它返回 null。
有什么建议吗?
(使用 Spring 3.0.5 和 CGLIB 2.2.2)
添加代码:
第一个注释是:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Produces {
ResultType[] value();
}
第二个注释是
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JamonMonitored {
String type() default "";
String tag() default "";
}
代码块是用来检查注解的
Collection<Method> candidates = Collections2.filter(Arrays.asList(executorInstance.getClass().getMethods()), new Predicate<Method>() {
@Override
public boolean apply(Method input) {
return input.getAnnotation(Produces.class) != null;
}
});
if (candidates.isEmpty()) {
// throws exception
}
如果我同时使用@Produces 和@JamonMonitored 注释一个方法,getAnnotation(Produces.class) 始终是null。
【问题讨论】:
-
能否提供代码。这看起来很有趣,想看看。但是没听说过这样的东西,有代码吗?
-
希望你已经完成了这部分 - stackoverflow.com/questions/1706751/…
标签: java spring reflection annotations cglib