我有几个类需要用名称进行注释,因此我将注释定义为
@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"
作为键,它是 Annotation 的值而不是 bean 名称。
有办法做到这一点吗?
只需获取注解对象并拉出值即可
Map<String,T> tmpMap = new HashMap<String,T>();
JsonUnmarshallable ann;
for (T o : applicationContext.getBeansWithAnnotation(annotationType).values()) {
ann = o.getClass().getAnnotation(JsonUnmarshallable.class);
tmpMap.put(ann.value(),o);
}
return o;
如果不清楚,请告诉我。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)