Sub topic 4
Auto-configuration: how Boot configures beans for you
This chapter assumes you know basic Java, understand what a Spring bean is, and have seen dependency injection (the container hands an object its collaborators instead of the object creating them). It also assumes you know what a starter is: a dependency that pulls in a curated set of libraries.
The problem it solves
In plain Spring, adding a web server or a database connection means declaring beans yourself. You write a DataSource bean, a DispatcherServlet bean, a JSON converter bean, and so on. Most applications declare nearly identical beans.
Auto-configuration is Boot's answer. At startup, Boot looks at what is on your classpath and what beans you already defined, then registers sensible default beans for the rest.
How it works
Boot ships many auto-configuration classes. Each one is an ordinary @Configuration class that is guarded by conditions. A condition is a test that must pass before a bean is registered.
@SpringBootApplicationincludes@EnableAutoConfiguration.- Boot reads the list of auto-configuration classes from the file
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsinside each library JAR that provides them. - Each class is evaluated. Its conditions are checked against the classpath, the existing beans, and your properties.
- Beans whose conditions pass are added to the application context.
The most common conditions are:
| Annotation | The configuration applies when |
|---|---|
| `@ConditionalOnClass` | A given class is on the classpath |
| `@ConditionalOnMissingBean` | You have not defined a bean of that type |
| `@ConditionalOnProperty` | A property has a given value |
| `@ConditionalOnWebApplication` | The app is a web application |
A worked example
Add spring-boot-starter-web and Tomcat plus Spring MVC land on the classpath. Boot sees DispatcherServlet and registers the MVC beans, an embedded Tomcat, and a Jackson JSON converter. You wrote none of that.
Now override one default. Because the default ObjectMapper is registered with @ConditionalOnMissingBean, defining your own replaces it:
package com.example.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JsonConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
return mapper;
}
}Boot's ObjectMapper backs off and yours is used. This "back off" behavior is the core idea: your explicit configuration always wins over the default.
Seeing what Boot decided
To see every decision, start the app with the debug flag:
java -jar target/demo-0.0.1-SNAPSHOT.jar --debugBoot prints a condition evaluation report with two main sections: positive matches (configurations that applied) and negative matches (configurations skipped, with the failed condition).
Common errors
- A bean you expected is missing. Check the negative matches in the debug report. Usually a starter or library is absent from the classpath.
- A default you did not expect appears. A transitive dependency (one pulled in by another library) added a class that triggered a condition.
- Your custom bean replaces too much. Defining your own
ObjectMapperalso discards Boot's property-driven defaults, such asspring.jackson.*settings. Prefer properties or a customizer bean when you only need a small change. - An unwanted configuration runs. Exclude it with
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)or the propertyspring.autoconfigure.exclude.
Exercise
Your application defines its own DataSource bean, and Boot's DataSourceAutoConfiguration is on the classpath. Which bean is used, and which mechanism makes that happen?
Show answer
Your DataSource is used. Boot's default DataSource is guarded by @ConditionalOnMissingBean, so when a bean of that type already exists, the auto-configuration backs off and does not register its own.
Exercise
Which flag shows you why a particular auto-configuration was or was not applied, and which section of its output lists the skipped ones?
Show answer
Run the app with --debug (or set debug=true). The condition evaluation report lists skipped configurations under "Negative matches", along with the condition that failed.