Sub topic 2
How Spring Boot finds auto-configuration classes
This chapter assumes you can write @Configuration and @Bean classes, run a basic Spring Boot application, and use application.properties. It covers how Spring Boot builds its list of auto-configuration candidates. Conditions are covered in the next chapters.
From @SpringBootApplication to a list of classes
@SpringBootApplication is a composed annotation. It combines three annotations:
@SpringBootConfiguration, a specialized@Configuration.@ComponentScan, which finds your own components.@EnableAutoConfiguration, which switches on auto-configuration.
@EnableAutoConfiguration imports a class named AutoConfigurationImportSelector. During startup, Spring calls this selector while it processes your configuration classes. The selector returns the names of classes to register as configuration. Those names come from a file on the classpath, not from component scanning.
This is the main difference between auto-configuration and your own beans. Your classes are found by scanning packages under the application class. Auto-configuration classes live in other jars and other packages, so scanning would never reach them. They are listed by name instead.
The AutoConfiguration.imports file
Each library that provides auto-configuration ships a text file at this path inside its jar:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsThe file contains one fully qualified class name per line. Lines that start with # are comments. For the greeting library used in this tutorial, the file has one line:
com.example.greeting.GreetingAutoConfigurationThe selector asks Spring's ImportCandidates helper to read every file with this name from every jar on the classpath. It merges the lines into one list and removes duplicates. Spring Boot's own spring-boot-autoconfigure jar contributes the largest share of that list.
The class itself is marked with @AutoConfiguration:
package com.example.greeting;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
@AutoConfiguration
public class GreetingAutoConfiguration {
@Bean
public GreetingService greetingService() {
return new GreetingService("Hello");
}
}@AutoConfiguration is a @Configuration with two differences. It marks the class as an auto-configuration, and it accepts the before and after ordering attributes, which a later chapter covers. Auto-configuration classes should not be component-scanned. Putting them in a package that your @ComponentScan covers causes them to be processed as ordinary configuration, and that loses the ordering and filtering that auto-configuration gets.
What the selector does with the list
Reading the file does not mean every class is applied. The selector narrows the list in steps:
- It reads all candidates from the
.importsfiles. - It removes classes named in
excludeorexcludeNameon@SpringBootApplication, or in thespring.autoconfigure.excludeproperty. - It applies a fast filter based on the classpath. Spring Boot reads a generated metadata file and drops candidates whose required classes are missing, without loading the class.
- It sorts the remaining classes into processing order.
- The remaining classes are imported, and each class's own conditions are evaluated as it is processed.
Steps 2 and 3 only shorten the list. The decision about individual beans happens in step 5, through the @Conditional family.
Older versions used spring.factories
Before Spring Boot 2.7, auto-configurations were registered in META-INF/spring.factories under the key org.springframework.boot.autoconfigure.EnableAutoConfiguration. Spring Boot 2.7 introduced the .imports file, and Spring Boot 3.0 removed support for the old key. If you maintain a library that targets Boot 3, move your entries to the .imports file. A common symptom of forgetting this is that your auto-configuration silently never runs, because nothing reads the old entry.
A common mistake
The file name and folder must match exactly. A typo such as META-INF/spring/AutoConfiguration.imports produces no error. The class is simply never a candidate. If your auto-configuration does not apply, check the path inside the built jar first, since resources placed outside src/main/resources do not get packaged.
Exercise
A teammate puts a class annotated with @AutoConfiguration in a package below the application class and does not add it to any .imports file. Is it processed as an auto-configuration? Explain.
Show answer
No. Because it sits under the application class's package, @ComponentScan picks it up as an ordinary configuration class, so it is processed with your own configuration and does not get auto-configuration ordering or filtering. Auto-configuration discovery relies on the name being listed in the AutoConfiguration.imports file, and it is not listed. The fix is to move it outside the scanned packages and register it in the .imports file.
Exercise
You upgrade a library to Spring Boot 3 and its auto-configuration stops running, with no error. Its entries are in META-INF/spring.factories. What do you change?
Show answer
Create META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports and list the class names there, one per line. Spring Boot 3 no longer reads the EnableAutoConfiguration key from spring.factories.