java - Spring - 是否有可能获取所有包,并在@ComponentScan 中注册?

标签 java spring spring-mvc

是否可以获取所有已注册到@ComponentScan 的包的列表? 我需要知道在我的 Spring Boot 应用程序中注册了哪些(root?)包...

最佳答案

可能的解决方案 - Scanning Java annotations at runtime

使用org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

应用程序接口(interface)

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_DEFAULT_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());

关于java - Spring - 是否有可能获取所有包,并在@ComponentScan 中注册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28014891/

相关文章:

java - 从不同于类路径的位置加载 spring XML 以在 Servlet 中获取 bean

java - Web服务的URL Get返回空参数

java - 找不到 [javax.sql.DataSource] 类型的合格 bean

json - 数据表错误 : "Requested unknown parameter"

java - 为什么接口(interface)和xml映射器文件必须在同一个包中并具有相同的名称?

Java InputStream读取多部分消息

java - Spring MVC 自定义方法参数绑定(bind)

java - CloudSolrClient 线程安全吗?

Java 小程序适用于 Oracle JRE 7u5,不适用于 7u6

spring - WebClient 与 RestTemplate