我有我需要用一个名字来注释,所以我定义我的注释为的Java注释扫描带弹簧
@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);
}
问题是返回的map包含["myClassInfo" -> object of MyClassInfo]
但我需要该映射包含"myClass"
作为键,这是Annotation的值而不是bean的名称。
有没有办法做到这一点?
我试图使用框架,它是更灵活,但是我无法找到特定于我的使用情况。你能告诉我怎样才能得到annotationDb返回一个由类 – Manoj
中定义的注解的值作为键值的Map对不起,我错了,但你可以发布过程那个Map –