Sub topic 9
How auto-configurations are discovered and registered
This chapter assumes you know beans, @Configuration and @Bean, and how to run a basic Spring Boot application. It covers how Boot finds auto-configuration classes and why a class on the classpath is not enough to make it run.
The discovery problem
Component scanning only covers the packages under your @SpringBootApplication class. An auto-configuration lives in a library, in a package your application never scans. Boot therefore needs a separate list of candidates.
If auto-configurations were found by scanning, they would behave like ordinary user configuration. They would load in an unpredictable order and could not reliably back off for your own beans. Boot avoids this by reading an explicit list from each jar.
The imports file
Each library that ships auto-configurations lists them in this file inside its jar:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsThe file has one fully qualified class name per line. Lines starting with # are comments.
com.example.greeting.GreetingAutoConfigurationThis is the registration step for the greeting example. Without this line, GreetingAutoConfiguration is just a class that nobody loads.
How Boot uses the file
@EnableAutoConfiguration imports a selector. At startup the selector does the following:
- Reads every
AutoConfiguration.importsfile on the classpath, one per jar. - Merges the lists and removes duplicates.
- Removes classes named in
excludeor inspring.autoconfigure.exclude. - Hands the remaining classes to the context as configuration candidates.
- Evaluates each class's conditions. Classes whose conditions fail are skipped.
Step 5 matters most. Being listed only makes a class a candidate. Whether it contributes beans depends on its conditions, such as @ConditionalOnClass and @ConditionalOnMissingBean.
Deferred processing
Boot processes auto-configurations after your own configuration classes. That ordering is what makes @ConditionalOnMissingBean work. By the time the auto-configuration runs, your bean definitions already exist, so the condition can see them and back off.
The @AutoConfiguration annotation marks a class as one of these candidates. It is a specialized @Configuration. Do not put @AutoConfiguration classes in a package your application scans. Scanning would load them as ordinary configuration and defeat the deferred ordering.
Key point about the older mechanism
Older Boot versions (before 2.7) registered auto-configurations in META-INF/spring.factories under the key org.springframework.boot.autoconfigure.EnableAutoConfiguration. Boot 2.7 introduced the imports file, and Boot 3 removed support for the old key. If you port a library from Boot 2, move the entries to the imports file. spring.factories still exists for other purposes, but not for this one.
Common errors
| Symptom | Likely cause |
|---|---|
| Bean from your library never appears | Class missing from the imports file, or the file is in the wrong directory |
| `ClassNotFoundException` for the listed class | Typo in the fully qualified name |
| Auto-configuration ignores your bean and creates its own | Class is component-scanned, so it runs too early |
| Works in Boot 2.x, silent in Boot 3 | Entry still in `spring.factories` only |
You can confirm registration with the conditions report from the previous chapter. Start with --debug and look for your class under positive or negative matches. If it appears nowhere, it was never registered. If it appears under negative matches, it was registered and a condition failed.
java -jar app.jar --debugExcluding a registered class
Because registration is just a list, you can remove entries without editing the jar:
# application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationExercise
Your library defines @AutoConfiguration public class AuditAutoConfiguration with a conditional bean, but the bean is never created and the conditions report does not mention the class. What is the most likely cause, and how do you fix it?
Show answer
The class was never registered. Add its fully qualified name on its own line to META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports in the library's resources. The conditions report only lists classes that Boot loaded as candidates.
Exercise
Why must auto-configuration classes stay out of the packages scanned by @SpringBootApplication?
Show answer
Component scanning would load them as ordinary configuration, before Boot's deferred auto-configuration processing. Conditions such as @ConditionalOnMissingBean could then evaluate before your own beans are defined, so the back-off behavior would stop working reliably.