【发布时间】:2012-05-19 22:30:44
【问题描述】:
我需要用名称注释的类很少,因此我将注释定义为
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface JsonUnmarshallable {
public String value();
}
现在需要这个注解的类被定义为
@JsonUnmarshallable("myClass")
public class MyClassInfo {
<few properties>
}
我使用下面的代码来扫描注释
private <T> Map<String, T> scanForAnnotation(Class<JsonUnmarshallable> annotationType) {
GenericApplicationContext applicationContext = new GenericApplicationContext();
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(applicationContext, false);
scanner.addIncludeFilter(new AnnotationTypeFilter(annotationType));
scanner.scan("my");
applicationContext.refresh();
return (Map<String, T>) applicationContext.getBeansWithAnnotation(annotationType);
}
问题是返回的映射包含["myClassInfo" -> object of MyClassInfo],但我需要映射包含"myClass" 作为键,这是注释的值而不是bean 名称。
有没有办法做到这一点?
【问题讨论】:
标签: java spring annotations applicationcontext