0

Ok, so I want to use a ClassPathScanningCandidateComponentProvider to find components that have a method that is annotated with a custom annotation.

I've done similar before. However, this time, I'm in a library that's being used by an application for which I can make no assumptions about the package names.

In particular, respectively annotated classes may be in both the library I'm working in and the application that's using it.

I.e.

object AnnotatedClassesFinder {
    @JvmStatic
    fun <A : Annotation> findClassesAnnotated(annotationClass: Class<A>): Collection<Class<*>> {
        return createScanner(annotationClass)
            .findCandidateComponents("com.app.my") //<-- need to find this constant (may be multiple)
            .map(BeanDefinition::getBeanClassName)
            .map { Class.forName(it) }
    }

    
    private fun <A : Annotation> createScanner(annotationClass: Class<A>): ClassPathScanningCandidateComponentProvider {
        return ClassPathScanningCandidateComponentProvider(false).apply {
            addIncludeFilter(AnnotationTypeFilter(annotationClass))
        }
    }
}

How do I get all base packages Spring is currently using for its component scan?

User1291
  • 7,664
  • 8
  • 51
  • 108
  • sorry i did not get your question completely ? what are you expecting as output ? you mean all classes in base package created by you ? – Ryuzaki L Sep 22 '20 at 17:58
  • @Deadpool No, all classes annotated with my annotation, both in the base packages of my library as well as in all base packages of the app (not written by me) that uses it. – User1291 Sep 22 '20 at 20:08

0 Answers0