Search code examples
javaspringspring-bootspring-beanspring-boot-starter

Is @SpringBootApplication able to find and AutoConfigure all dependency's beans without META-INF/spring.factories?


Why do we need META-INF/spring.factories when create starters for Spring Boot applications? What if omit it at all or leave empty?

Doesn't the target application's @SpringBootApplication which is

a combination of three annotations @Configuration (used for Java-based configuration), @ComponentScan (used for component scanning), and @EnableAutoConfiguration

scan everything and find all beans from all the starters with no help of META-INF/spring.factories?


Solution

  • Component Scanning would scan the packages that you give it. You could technically tell it to scan all the packages of your dependencies, too, and it would start loading up any beans defined in them. If you don’t specify any packages to scan, then Spring will use the base package where the annotation is applied, which would very likely not include beans defined in any dependency libs.

    There’s another layer to this- a lot of the libraries you use may be using annotations like “@AutoConfigureBefore” to give spring instructions on the order of bean creation. Component Scanning will not respect that, which could result in some weird behaviors if some dependency tries to override a bean from another which is annotated with @ConditionalOnMissingBean (I.e. create this bean only if it doesn’t exist.) You could easily end up with name collision issues where that bean actually gets created first, and then the override bean is created, too.

    So the answer seems to be no. You need spring.factories.