Spring - is it possible to get all the packages registered in @ComponentScan?

Can I get a list of all packages registered in @ComponentScan? I need to know which packages (root?) Have been registered in my Spring application to download ...

+4
source share
2 answers

Possible Solution - Scan Java Runtime Annotations

Use org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

API

A component provider that scans the class path from the base package. This then applies the exceptions and filters on the resulting classes in 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
source

maybe a long snapshot, but each @ComponentScan should be used with @Configuration (it's just a different kind of spring bean). Thus, you can list all the beans from the application context and check through the reflection from which they have @ComponentScan and get its value.

+1
source

Source: https://habr.com/ru/post/1267011/


All Articles