【发布时间】:2015-03-11 20:53:49
【问题描述】:
我只想找出“Spring bean 中所有被注释为@Versioned 的类/方法”。
我将自定义注释创建为,
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Versioned {
.....
}
当我使用 Java 反射查找方法时,此注释完美运行:
for(Method m: obj.getClass().getMethods()){
if(m.isAnnotationPresent(Versioned.class)){
.... // Do something
}
但是当我访问 Spring bean 并尝试类似的检查时它不起作用:
public class VersionScanner implements ApplicationContextAware{
public void setApplicationContext(ApplicationContext applicationContext){
for(String beanName: applicationContext.getBeanDefinitionNames()){
for(Method m: applicationContext.getBean(beanName).getClass().getDeclaredMethods()){
if(m.isAnnotationPresent(Versioned.class){
// This is not WORKING as expected for any beans with method annotated
}
}
}
}
}
其实这段代码确实找到了@RequestMapping等其他注解。我不确定我的自定义注释做错了什么。
【问题讨论】:
-
bean 被代理了吗?
-
我想说不。但 SOF 至少需要 10 个字符 :-)
-
好的,请提供一个可重现的最小示例。
-
您将 @Versioned 注释应用于什么类类型的方法?
标签: java spring spring-mvc