Spring – 是否有可能获得所有使用@ComponentScan注册的软件包?

是否可以获得在@ComponentScan注册的所有包的列表? 我需要知道,我的Spring Boot应用程序中注册了什么(root?)包…

也许是长镜头,但每个@ComponentScan都应该与@Configuration一起使用(这只是另一种spring bean)。 因此,您可以枚举应用程序上下文中的所有bean,并通过reflection检查哪些bean具有@ComponentScan并获取其值。

可能的解决方案 – 在运行时扫描Java注释

使用org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

API

从基础包扫描类路径的组件提供程序。 然后,它会对结果类应用排除和包含filter以查找候选项。

 ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(); scanner.addIncludeFilter(new AnnotationTypeFilter(.class)); for (BeanDefinition bd : scanner.findCandidateComponents()) System.out.println(bd.getBeanClassName());