Sub topic 4
How auto-configuration creates beans from the classpath
This chapter assumes you know what a bean and the application context are, and that you can read @Configuration and @Bean code. It also assumes you have seen @SpringBootApplication on a main class. Here you will learn what happens between that annotation and the finished set of beans.
Auto-configuration is ordinary configuration
An auto-configuration class is a regular Spring @Configuration class that ships inside a library jar. It declares @Bean methods, and conditions decide whether each class and method applies. Boot adds no special bean mechanism. It adds a way to find these classes and a set of conditions that guard them.
Here is a small example in the style Boot uses:
@AutoConfiguration
@ConditionalOnClass(GreetingService.class)
public class GreetingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public GreetingService greetingService() {
return new GreetingService("Hello");
}
}@AutoConfiguration is a specialized @Configuration. Boot marks these classes with it so it can treat them differently from your own configuration. It also sets proxyBeanMethods to false, because these classes do not call their own @Bean methods.
The discovery process
@SpringBootApplication includes @EnableAutoConfiguration. That annotation imports a selector, AutoConfigurationImportSelector, which runs when the context processes your configuration classes. The selector does these steps:
- It reads every file named
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importson the classpath. Each line names one auto-configuration class. - It merges the lists from all jars, removes duplicates, and removes any class you excluded.
- It filters the candidates using the cheap class-level conditions, mainly
@ConditionalOnClass, read from class metadata. Classes whose required types are missing are dropped without being loaded. - It hands the survivors to the context, which registers them as configuration classes.
- The context evaluates the remaining conditions, such as
@ConditionalOnMissingBean, as it processes each class and each@Beanmethod.
Why the classpath drives the result
The conditions look at what is on the classpath, so your dependencies decide which beans exist. Add spring-boot-starter-web and Spring MVC classes appear on the classpath. The MVC auto-configurations then pass their @ConditionalOnClass checks and register their beans. Remove the dependency and the same auto-configurations are skipped. Nothing in your code changes.
The result is a division of labor. A starter puts classes on the classpath, and auto-configuration reacts to them. The starter does not create beans itself.
Your configuration is processed first
The import selector is a deferred import selector. Boot processes auto-configurations after all of your own configuration classes. By the time @ConditionalOnMissingBean runs, your beans are already registered. That is how your definition wins over the default without any special override syntax.
@Configuration
public class MyConfig {
@Bean
public GreetingService greetingService() {
return new GreetingService("Hi");
}
}With this class in your application, the condition on the auto-configured greetingService method fails and Boot skips it.
Common errors
- Auto-configuration class is not found. Putting it under component scanning does not register it, and Boot's own guidance is to keep these classes out of scanned packages. List it in the
.importsfile instead. - A default bean appears even though you defined one. Your bean may be registered under a different type, so
@ConditionalOnMissingBeandoes not see it as a match. Check the return type of your@Beanmethod. - An old
spring.factoriesregistration is ignored. Since Boot 2.7, auto-configurations go in the.importsfile. Boot 3 no longer readsspring.factoriesfor this purpose.
Exercise
Your application defines its own DataSource bean, but you still see an auto-configured one in the logs. Which two things would you check first?
Show answer
First, check that your @Bean method returns a type that matches what the auto-configuration's @ConditionalOnMissingBean looks for, usually DataSource. Second, check that your configuration class is actually part of the application context, for example that it sits in a scanned package. Auto-configurations run after your configuration, so a registered bean of the right type makes the default back off.