Java annotation scanning with spring

女生的网名这么多〃 提交于 2019-12-01 11:18:56

问题


I have few classes that I need to annotate with a name so I defined my annotation as

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface JsonUnmarshallable {
    public String value();
}

Now the class that needs this annotation is defined as

@JsonUnmarshallable("myClass")
public class MyClassInfo {
<few properties>
}

I used below code to scan the annotations

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);
}

The problem is that the map returned contains ["myClassInfo" -> object of MyClassInfo] but I need the map to contain "myClass" as key, which is the value of the Annotation not the bean name.

Is there a way of doing this?


回答1:


Just get the annotation object and pull out the value

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;

Let me know if that's not clear.




回答2:


In my case I wrote like below:

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(JsonUnmarshallable.class));
Set<BeanDefinition> definitions = scanner.findCandidateComponents("base.package.for.scanning");

for(BeanDefinition d : definitions) {
    String className = d.getBeanClassName();
    String packageName = className.substring(0,className.lastIndexOf('.'));
    System.out.println("packageName:" + packageName + " , className:" + className);
}



回答3:


Maybe you can use http://scannotation.sourceforge.net/ framework to achive that.

Hope it helps.




回答4:


You can provide a custom BeanNameGenerator to the ClassPathBeanDefinitionScanner, which can look for the value of your annotation and return that as the bean name.

I think an implementation along these lines should work for you.

package org.bk.lmt.services;

import java.util.Map;
import java.util.Set;

import org.springframework.context.annotation.AnnotationBeanNameGenerator;
public class CustomBeanNameGenerator extends AnnotationBeanNameGenerator{
    @Override
    protected boolean isStereotypeWithNameValue(String annotationType,
            Set<String> metaAnnotationTypes, Map<String, Object> attributes) {

        return annotationType.equals("services.JsonUnmarshallable");
    }
}

Add this to your previous scanner code: scanner.setBeanNameGenerator(new CustomBeanNameGenerator());



来源:https://stackoverflow.com/questions/10549458/java-annotation-scanning-with-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!