User1291
User1291

Reputation: 8209

Figure out where spring application is scanning for components

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?

Upvotes: 0

Views: 237

Answers (0)

Related Questions