Java annotation scanning with spring

后端 未结 4 1487
Happy的楠姐
Happy的楠姐 2021-01-07 04:37

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 @i         


        
4条回答
  •  既然无缘
    2021-01-07 05:14

    In my case I wrote like below:

    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(JsonUnmarshallable.class));
    Set 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);
    }
    

提交回复
热议问题