且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在运行时扫描 Java 注释

更新时间:2023-01-10 18:33:21

使用 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

API

从基本包扫描类路径的组件提供程序.然后将排除和包含过滤器应用于结果类以查找候选者.

A component provider that scans the classpath from a base package. It then applies exclude and include filters to the resulting classes to find candidates.

ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(<DO_YOU_WANT_TO_USE_DEFALT_FILTER>);

scanner.addIncludeFilter(new AnnotationTypeFilter(<TYPE_YOUR_ANNOTATION_HERE>.class));

for (BeanDefinition bd : scanner.findCandidateComponents(<TYPE_YOUR_BASE_PACKAGE_HERE>))
    System.out.println(bd.getBeanClassName());