Sub topic 1
What auto-configuration does
Spring Boot builds many of an application's beans for you, based on what it finds at startup. This chapter explains what that means and how the pieces fit together. Later chapters cover each piece in detail.
Prerequisites
This chapter assumes you know the following:
- Java and Maven or Gradle basics.
- Spring dependency injection, plus
@Configurationand@Beanclasses. - How to run a basic Spring Boot application.
- Externalized configuration with
application.properties.
The problem it solves
In plain Spring, a web application with a database needs many beans. You declare a DataSource, a transaction manager, a JSON mapper and a servlet container, and each needs settings. Most applications declare nearly the same beans in nearly the same way.
Auto-configuration moves that repeated work into the framework. Boot ships many @Configuration classes, called auto-configuration classes. Each one declares the beans for one feature, such as JDBC or Jackson. Boot applies a class only when the application looks like it needs that feature.
How it is switched on
A standard application has one annotation and one main method:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}@SpringBootApplication combines three annotations:
@SpringBootConfiguration, which marks the class as a configuration class.@ComponentScan, which finds your own components.@EnableAutoConfiguration, which turns on auto-configuration.
@EnableAutoConfiguration is the one that matters here. It tells Spring to load the list of auto-configuration candidates and process them along with your own configuration.
From candidates to beans
Processing has three steps:
- Boot reads a list of candidate classes from files on the classpath. The next chapter covers this.
- Each candidate is checked against its conditions. A condition is a test such as "this class is on the classpath" or "no bean of this type exists yet."
- Only candidates whose conditions pass register their beans.
Most candidates are skipped in any given application. Your application doesn't use every feature Boot supports, so most conditions fail. That is the intended design.
Your configuration wins
Auto-configuration provides defaults, not requirements. If you define a bean yourself, Boot's version usually steps back. This is called backing off. For example, Boot's Jackson support creates an ObjectMapper only if you haven't defined one:
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() {
return new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
}
}With this class in place, the application uses your ObjectMapper. Boot's own is never created. You only write code for the parts you want to change.
Backing off is a convention that each auto-configuration implements with a condition, not something the framework does on its own. Later chapters show how.
What the starters add
A starter, such as spring-boot-starter-web, is a dependency that pulls in a group of libraries. Adding one changes the classpath, and many conditions test the classpath. So a dependency change can switch on a whole group of beans without any code change.
Why this chapter matters for the rest
Two questions drive the rest of this tutorial:
- Which classes does Boot consider, and in what order does it process them?
- Which conditions decide whether each class applies?
Order matters because some conditions look at beans that already exist. A class processed early sees fewer beans than one processed later. Later chapters show how to read the report that records every decision, how to write your own auto-configuration, and how to test it.
Exercise
An application has no ObjectMapper bean of its own, and Jackson is on the classpath. A developer then adds a @Bean ObjectMapper method to a @Configuration class. What happens to Boot's ObjectMapper, and which annotation is responsible for turning auto-configuration on?
Show answer
Boot's ObjectMapper is no longer created, because its auto-configuration backs off when a bean of that type already exists. Auto-configuration is turned on by @EnableAutoConfiguration, which @SpringBootApplication includes.