Sub topic 9
How auto-configurations are discovered and registered
This chapter assumes you know beans, @Configuration, @Bean, and how to run a Spring Boot application. It also assumes you have seen @SpringBootApplication and a conditional annotation such as @ConditionalOnMissingBean. The chapter covers how Boot finds auto-configuration classes, why a class can exist on the classpath and still never run, and what goes wrong when registration is done incorrectly.
The problem registration solves
An auto-configuration is an ordinary @Configuration class. Boot cannot find it by component scanning. Your application scans only its own packages, starting from the package of the @SpringBootApplication class. An auto-configuration lives in a library, in a package your code never mentions.
Boot therefore needs a separate list of candidates. Every library that ships auto-configuration publishes that list in a known file inside its JAR. At startup, Boot reads that file from every JAR on the classpath and merges the results.
The AutoConfiguration.imports file
Since Spring Boot 2.7, the list lives at this path inside the JAR:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsThe file is plain text. Each line holds one fully qualified class name. Blank lines are ignored, and lines starting with # are comments. For the example from this tutorial, the file contains one line:
com.example.greeting.GreetingAutoConfigurationA library with several auto-configurations lists each on its own line:
# Greeting library auto-configurations
com.example.greeting.GreetingAutoConfiguration
com.example.greeting.GreetingMetricsAutoConfigurationIn a Maven or Gradle project, the file belongs in src/main/resources/META-INF/spring/. The build copies it into the JAR unchanged.
Why the file name looks odd
The file name is the fully qualified name of the @AutoConfiguration annotation. Spring Boot uses the same mechanism for several lists, and the file name states which list it is. ImportCandidates.load(AutoConfiguration.class, classLoader) is the API that reads it.
The older spring.factories mechanism
Before 2.7, auto-configurations were registered in META-INF/spring.factories under a key:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.greeting.GreetingAutoConfigurationSpring Boot 2.7 deprecated this form for auto-configuration, and Spring Boot 3.0 removed support for it. Two points matter in practice:
- A library built for Boot 2.x that still uses
spring.factorieswill silently stop auto-configuring on Boot 3. No error appears. The beans are simply missing. spring.factoriesstill exists for other extension points, such asApplicationListenerandEnvironmentPostProcessorregistration. Only theEnableAutoConfigurationkey moved.
If you maintain a library for both generations, ship both files. Boot 2.7 reads either, and Boot 3 reads only the .imports file.
How Boot loads the candidates
The registration path has a few steps, and each one explains a symptom you may see later.
@SpringBootApplicationincludes@EnableAutoConfiguration.@EnableAutoConfigurationimportsAutoConfigurationImportSelector.- The selector loads every
AutoConfiguration.importsfile on the classpath and merges the class names into one list. - It removes duplicates.
- It removes anything excluded by the
excludeattribute,excludeName, or thespring.autoconfigure.excludeproperty. - It applies a filter that checks the class-level conditions from metadata, without loading the classes.
- It sorts the survivors by their ordering rules.
- It hands the sorted list to the Spring container as configuration classes to process.
Two stages evaluate conditions. Step 6 is a cheap pre-filter. Step 8 and the later bean registration evaluate the full set of conditions, including @ConditionalOnMissingBean, which can only be decided once user-defined beans are known. The conditions report from the earlier chapter shows the final outcome of both.
The pre-filter step
Loading every candidate class would be slow and could fail, because a candidate may reference classes that are not on the classpath. Boot avoids this with a generated metadata file.
When a library is built, the spring-boot-autoconfigure-processor annotation processor writes META-INF/spring-autoconfigure-metadata.properties. It records the class-level conditions (@ConditionalOnClass, @ConditionalOnBean, @ConditionalOnWebApplication) and ordering attributes for each auto-configuration. At startup Boot checks these values against the classpath without loading the configuration classes. Most non-matching candidates are dropped at this point, which keeps startup fast even though Boot ships well over a hundred auto-configurations.
The processor is optional for your own libraries. Without it, everything still works, but each candidate is loaded and evaluated individually.
Why @AutoConfiguration and not @Configuration
You should mark auto-configuration classes with @AutoConfiguration, introduced in Boot 2.7. It is a specialized @Configuration with two extra properties:
- It sets
proxyBeanMethods = false, so@Beanmethods are not proxied. Auto-configurations should not call their own@Beanmethods to wire dependencies. They should take dependencies as method parameters instead. - It accepts
before,after,beforeName, andafterNameattributes for ordering. Ordering is covered in the next chapter.
Marking your class @Configuration and listing it in the imports file works, but you lose the ordering attributes.
Auto-configuration must stay out of component scanning
This is the most common registration mistake. Consider this layout:
com.example.app <- @SpringBootApplication here
com.example.app.config <- GreetingAutoConfiguration accidentally hereIf an auto-configuration lives inside the package tree that your application scans, component scanning finds it as a normal configuration. Several things then go wrong:
- It is processed as a regular configuration, so its position in the auto-configuration ordering is lost.
- Its
@ConditionalOnMissingBeanchecks can run before your own beans are registered, and back-off becomes unreliable. - It is applied even if you list it in
spring.autoconfigure.exclude, because the exclusion affects only the import list, not scanning.
For this reason, @SpringBootApplication includes an AutoConfigurationExcludeFilter in its component scan. The filter skips classes that appear in the imports files. This protects you when the class is both scanned and registered. It does not protect a class that is scanned but not listed, and such a class behaves as ordinary configuration.
The rule for library authors is to put auto-configuration classes in a package that no application will scan. Never place them in the same package as the user's @SpringBootApplication class, and never rely on @ComponentScan inside them.
Registration is also the exclusion point
Because auto-configurations form a list before any bean exists, the list can be edited. The three ways to remove a candidate are:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}# application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationThe third option is excludeName, which takes a class name as a string. Use it when the class may not be on the compile classpath.
If you name a class in exclude that is not an auto-configuration, Boot fails at startup with an IllegalStateException. It states that the class is not annotated with @EnableAutoConfiguration candidates. This check catches typos early.
Verifying that registration worked
When your auto-configuration does not apply, work down this list before changing any code:
- Confirm the imports file is in the JAR:
jar tf greeting-spring-boot-autoconfigure.jar | grep AutoConfiguration.imports. - Confirm the path and file name are exact, including
META-INF/spring/and the full file name. A misspelled name is ignored without any message. - Confirm each line is a fully qualified class name with no trailing characters.
- Start the application with
--debugand search the conditions report for your class name.
java -jar app.jar --debugThe result tells you which stage failed:
| Report result | Meaning |
|---|---|
| Class absent from the report | It was never registered or was excluded. Check the imports file. |
| Listed under negative matches | It was registered, and a condition failed. Read the reason. |
| Listed under positive matches | It was registered and applied. |
| Listed under exclusions | A property or annotation removed it. |
Registering imports for other purposes
The same file-based approach applies to a few other lists. You do not need them for ordinary auto-configuration, but the pattern recurs:
- Test slice auto-configurations are listed in files such as
META-INF/spring/org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc.imports. @ImportAutoConfigurationimports specific auto-configurations directly, which is useful in tests where you want only a few.
@ImportAutoConfiguration is the tool to use when you want an auto-configuration applied with its ordering rules but without the full @EnableAutoConfiguration machinery:
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(GreetingAutoConfiguration.class)
class TestConfig {
}Exercise
A teammate migrates a library from Spring Boot 2.6 to Boot 3. The library's auto-configuration class is unchanged and still listed under the org.springframework.boot.autoconfigure.EnableAutoConfiguration key in META-INF/spring.factories. The application starts without errors, but the library's beans are missing and the conditions report never mentions the class. What is the cause, and what is the fix?
Show answer
Spring Boot 3 no longer reads the EnableAutoConfiguration key from spring.factories, so the class is never a candidate. Because it is not registered, it does not appear in the conditions report and no error is raised. The fix is to create META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports containing the fully qualified class name on its own line, and to annotate the class with @AutoConfiguration.
Exercise
Your auto-configuration GreetingAutoConfiguration is in com.example.app.config, and your @SpringBootApplication class is in com.example.app. Users report that excluding it with spring.autoconfigure.exclude has no effect. Why, and how do you fix it?
Show answer
Component scanning picks the class up as a normal configuration because it sits under the application's scanned package, so removing it from the auto-configuration import list does not stop it. Move the class to a package outside any application's scan, for example com.example.greeting.autoconfigure, and keep it registered in the imports file.