Is it possible to get list of all packages, registered with @ComponentScan? I need to know, what (root?) packages have been registered in my Spring Boot application...
Asked
Active
Viewed 3,370 times
2 Answers
2
Possible solution - Scanning Java annotations at runtime
Use 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_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());
1
maybe long shot, but each @ComponentScan should be used with @Configuration (which is just another kind of spring bean). So you can enumerate all beans from application context and check via reflection which of them have @ComponentScan and get its value.