Search code examples
springspring-bootspring-annotations

Configurable Component Scan


Is there a way to make the component scan configurable externally or through an intermediate resolver class? My requirement is that a common library should include one or more of other smaller facilities (each having their own controller, services etc.) depending on whether those are "configured" or needed - e.g. in application properties.

The closest I can see a possibility of designing this is to declare a @Configuration class in the common library and keep it in the component scan class path (always). In this class I need some way to say that the following are the allowed scan paths (based on how downstream projects have configured their application properties).

Seems like TypeFilter custom implementation should do it. But how do I read application properties from inside the type filter implementation (annotation takes only the .class, so Spring must be initializing it.

Any other ways? Thanks!

Regards, Arnab.


Solution

  • This document describes how to create your own Auto-Configuration. It allows you to read properties and utilize several variations of @Conditional annotation.