Sub topic 1
What auto-configuration does
Spring Boot applications start with very little configuration code. You add a dependency, and a web server, a JSON mapper, or a connection pool appears. This chapter explains what produces those beans and how to reason about them. The later chapters in this tutorial cover the details: discovery, conditions, ordering, and testing.
Prerequisites
This chapter assumes you already know:
- Java and either Maven or Gradle well enough to add a dependency and run a build.
- Spring dependency injection, including
@Configurationclasses and@Beanmethods. - How to run a basic Spring Boot application with
SpringApplication.run. - Externalized configuration with
application.propertiesorapplication.yml.
The examples target Spring Boot 3.x. Where behavior differs in older versions, the text says so.
The problem it solves
In plain Spring, using a database means you write the infrastructure yourself. You define a DataSource, a JdbcTemplate that uses it, and a transaction manager. Every project repeats the same code with slightly different values.
Spring Boot moves that repetition into the libraries. It ships ordinary @Configuration classes that define these beans for you. Each class is guarded by conditions. A condition is a test that runs while the application context is being built. The class applies only when its conditions pass.
This design has one rule that governs everything else: your own configuration wins. Boot supplies sensible defaults, and each default steps aside when you define something yourself.
What an auto-configuration is
An auto-configuration is a regular Spring configuration class with two differences:
- It is not found by component scanning. It is listed by name in a file inside a library's jar.
- Its beans and the class itself carry conditions that decide whether they are registered.
Most of Boot's own auto-configurations live in the spring-boot-autoconfigure jar. Examples include DataSourceAutoConfiguration, JacksonAutoConfiguration, and WebMvcAutoConfiguration. Third-party libraries can ship their own by following the same mechanism, and you can do the same in your own modules. Chapter 10 of this tutorial builds one.
A starter, such as spring-boot-starter-web, is a different thing. A starter is a dependency bundle that pulls in libraries. The libraries on the classpath are what the auto-configuration conditions then react to. Adding a starter does not run any configuration by itself.
How it is switched on
Auto-configuration starts from the annotation on your main class. @SpringBootApplication is a composed annotation that combines three others:
| Annotation | Role |
|---|---|
| `@SpringBootConfiguration` | Marks the class as a configuration class (a specialization of `@Configuration`). |
| `@ComponentScan` | Finds your `@Component`, `@Service`, `@Configuration`, and similar classes in the package and below. |
| `@EnableAutoConfiguration` | Turns on the auto-configuration mechanism. |
@EnableAutoConfiguration imports a selector, AutoConfigurationImportSelector. The selector reads the candidate class names from files named META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports on the classpath. Spring then registers those classes as configuration classes, and their conditions are evaluated.
The selector is a deferred import selector. This means the candidates are processed after all of your own configuration classes. That timing is why a condition such as "no bean of this type exists yet" can see the beans you declared. The next chapters return to this point.
In Spring Boot versions before 2.7, the candidates were listed under the EnableAutoConfiguration key in META-INF/spring.factories. Boot 3.0 removed that mechanism for auto-configurations, so the .imports file is the only location.
The decision flow
Every candidate goes through the same steps. The following diagram shows the path from your main class to the final set of beans.
Two levels of conditions appear in the diagram. A condition on the class can disable all of its beans at once. A condition on a @Bean method disables only that bean. A typical auto-configuration uses both.
A worked example: watching a default back off
The clearest way to see auto-configuration is to observe one default and then replace it. This example uses the ObjectMapper, the Jackson class that converts objects to JSON. Add spring-boot-starter-web to a new project. Then use this main class:
package com.example.demo;
import java.util.Arrays;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
String[] names = context.getBeanNamesForType(ObjectMapper.class);
System.out.println("ObjectMapper beans: " + Arrays.toString(names));
context.close();
}
}You wrote no ObjectMapper definition, yet the program prints a bean:
ObjectMapper beans: [jacksonObjectMapper]JacksonAutoConfiguration created it because Jackson is on the classpath (a transitive dependency of the web starter) and no other ObjectMapper bean existed. It also applied any spring.jackson.* properties you set.
Now define your own ObjectMapper in a separate class in the same package:
package com.example.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MapperConfiguration {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}Run the program again:
ObjectMapper beans: [objectMapper]Only your bean exists. The auto-configured one backed off because its @Bean method is marked @ConditionalOnMissingBean. The condition checked the registered bean definitions, found yours, and skipped the default.
The example also shows the cost of replacing a default. Boot's ObjectMapper was built from the spring.jackson.* properties and from any Jackson2ObjectMapperBuilderCustomizer beans. Your plain new ObjectMapper() ignores both. When you only want to adjust the default, a customizer bean is usually a better choice than replacing the whole bean.
The kinds of signals conditions read
An auto-configuration decides based on facts about the running application. The following table lists the main ones. Chapters 3 to 7 cover each in depth.
| Signal | Typical annotation | Example question |
|---|---|---|
| A class on the classpath | `@ConditionalOnClass` | Is Jackson present? |
| A bean already defined | `@ConditionalOnMissingBean`, `@ConditionalOnBean` | Did the user already define an `ObjectMapper`? |
| A property value | `@ConditionalOnProperty` | Is `greeting.enabled` set to `false`? |
| The kind of application | `@ConditionalOnWebApplication` | Is this a servlet web application? |
Common misunderstandings
Starters are not auto-configuration. A starter only adds dependencies. If a feature does not activate, the cause may be a missing class (the conditions fail), not a missing starter setting.
Auto-configuration is not component scanning. Your classes are found by scanning the packages under your main class. Auto-configuration classes are found by name from the .imports files. If you place an auto-configuration inside a scanned package, Spring treats it as ordinary configuration, and the ordering guarantees described above no longer apply.
Auto-configuration does not override your beans. If a feature seems to ignore your settings, check whether you defined a bean that made the default back off. The default's property handling went away with it.
It is not hidden behavior. An auto-configuration is plain Java source. When something is unclear, you can open the class, read its conditions, and know why it applied. Chapter 11 shows how to get the same answer from the application itself with the --debug report.
What comes next
This chapter gave the overall model: candidates are discovered from a file, conditions filter them, and user-defined beans take priority. The next chapters examine each part. Chapter 2 looks at how the .imports files are located and read. Chapter 3 explains the @Conditional and Condition types that every conditional annotation builds on.
Exercise
A teammate says: "I added spring-boot-starter-data-jpa, so the JPA configuration runs." Using what this chapter covers, describe what actually happens, and name one reason the JPA auto-configuration might still not apply.
Show answer
The starter adds libraries to the classpath. Separately, @EnableAutoConfiguration (part of @SpringBootApplication) loads the candidate auto-configuration class names from the AutoConfiguration.imports files. The JPA auto-configuration classes are among those candidates, and each one is applied only if its conditions pass. It might not apply because a condition fails. For example, a required class is missing from the classpath, a property switches it off, or the application defined its own bean of the type the default would create, so the default backs off.
Exercise
In the worked example, why did the auto-configured ObjectMapper disappear once MapperConfiguration was added? And what do you lose by returning a plain new ObjectMapper() from your own bean method?
Show answer
The auto-configured bean method is guarded by @ConditionalOnMissingBean. Auto-configurations are processed after your own configuration, so the condition can see your ObjectMapper bean definition, finds it, and skips the default. Your plain new ObjectMapper() ignores the spring.jackson.* properties and any Jackson2ObjectMapperBuilderCustomizer beans that the default bean would have applied. A customizer bean adjusts the default without replacing it.