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
  • 3,253
  • 7
  • 43
  • 75

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
  • 10,377
  • 11
  • 69
  • 89
Arthur
  • 3,253
  • 7
  • 43
  • 75
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,675
  • 2
  • 29
  • 47