6

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...

Arthur
  • 2,993
  • 6
  • 37
  • 72

2 Answers2

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());
rjdkolb
  • 9,403
  • 8
  • 65
  • 80
Arthur
  • 2,993
  • 6
  • 37
  • 72
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.

Community
  • 1
  • 1
sodik
  • 4,605
  • 2
  • 27
  • 43