Sub topic 2
How Spring Boot finds auto-configuration classes
This chapter covers the discovery step: how Spring Boot turns the single @SpringBootApplication annotation into a list of auto-configuration classes. It assumes you can write @Configuration and @Bean classes, run a basic Spring Boot application, and set values in application.properties. It uses the Spring Boot 3.x mechanism. Conditions and ordering get their own chapters, so they appear here only where they affect discovery.
From one annotation to a candidate list
@SpringBootApplication is a composed annotation. It combines three others:
| Annotation | Role |
|---|---|
| `@SpringBootConfiguration` | Marks the class as a configuration class (a specialized `@Configuration`). |
| `@ComponentScan` | Scans the application's own packages for components. |
| `@EnableAutoConfiguration` | Turns on auto-configuration. |
Only @EnableAutoConfiguration is relevant here. It carries @Import(AutoConfigurationImportSelector.class). An import selector is a Spring hook that returns the names of configuration classes to register. AutoConfigurationImportSelector returns the names of the auto-configuration classes that Spring Boot will consider.
The selector implements DeferredImportSelector. Spring processes deferred selectors after all the application's own @Configuration classes have been parsed. This means your beans are already registered when auto-configuration runs. That timing is what lets an auto-configuration back off when you define a bean yourself.
The following class is a complete application entry point:
package com.example.app;
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);
}
}Nothing in this class names an auto-configuration. The names come from files inside the jars on the classpath.
The AutoConfiguration.imports file
Each jar that supplies auto-configurations lists them in a resource file:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsThe format is one fully qualified class name per line. Lines starting with # are comments, and blank lines are ignored. A library with one auto-configuration has a file like this:
# Auto-configurations for the greeting library
com.example.greeting.GreetingAutoConfigurationspring-boot-autoconfigure, the jar that ships Boot's own auto-configurations, has the same kind of file with a few hundred entries. Your own libraries use the same mechanism, which is what makes a custom starter possible.
The selector loads every file with this name from the classpath. It uses ImportCandidates, which calls the class loader's equivalent of getResources and so finds the file in every jar, not just the first. All the lists are merged.
Where this file replaced spring.factories
Older Boot versions registered auto-configurations in META-INF/spring.factories, under the key org.springframework.boot.autoconfigure.EnableAutoConfiguration. Boot 2.7 introduced the .imports file and deprecated that key. Boot 3.0 removed support for it. If you port a library from Boot 2.x and its auto-configuration never applies, check this first. The old spring.factories entry is silently ignored.
The discovery pipeline
After loading the names, the selector does not instantiate or register everything. It narrows the list in stages:
- Load the names from all
.importsfiles. - Remove duplicates. If two jars list the same class, it is kept once.
- Apply exclusions from
@SpringBootApplication(exclude = ...),excludeName, and thespring.autoconfigure.excludeproperty. - Filter using fast checks on class names and metadata, before any class is loaded.
- Fire an event so listeners can see the final list.
- Sort the survivors by order and register them for the normal configuration parsing.
Filtering before loading classes
Step 4 matters for startup time. Boot's own jar lists hundreds of auto-configurations. Most are irrelevant to a given application because their libraries are not on the classpath. Loading every class just to evaluate @ConditionalOnClass would be slow, and could fail if the annotation refers to a missing class.
To avoid that, Boot reads a metadata file generated at build time, META-INF/spring-autoconfigure-metadata.properties. It records the simple class-level conditions for each auto-configuration: required classes, required beans, and whether the application must be a web application. Three AutoConfigurationImportFilter implementations use this metadata:
OnClassConditiondrops a candidate if a class it requires is not on the classpath.OnBeanConditiondrops a candidate if a bean type it requires is not present.OnWebApplicationConditiondrops a candidate if the web application type does not match.
These filters only reject candidates early. The real @Conditional annotations are still evaluated later, on the classes that remain.
If you write your own library, the spring-boot-autoconfigure-processor annotation processor generates this metadata for you. Add it as an optional dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>The processor is an optimization. Without it, your auto-configuration still works, because the full conditions are evaluated later. It just misses the early-rejection shortcut.
What makes a class an auto-configuration
Being listed in the .imports file is what makes a class an auto-configuration candidate. The annotation is a convention that adds behavior. Annotate these classes with @AutoConfiguration, which is meta-annotated with @Configuration(proxyBeanMethods = false):
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 also has before, beforeName, after, and afterName attributes, which a later chapter uses for ordering.
Keeping auto-configurations out of component scanning
The @ComponentScan inside @SpringBootApplication has an exclude filter, AutoConfigurationExcludeFilter. It skips any class that is both a configuration class and listed in the .imports files. This protects the ordering guarantee. If component scanning found an auto-configuration, Spring would process it as ordinary application configuration, before the deferred step. Its conditions would then see an incomplete set of bean definitions, and @ConditionalOnMissingBean could not tell whether your bean would arrive later.
For a library, that means placing the auto-configuration in a package such as com.example.greeting, and having applications use a different base package. Even when package names overlap, the exclude filter protects you. The rule still holds as a design guideline.
Exclusions in the discovery step
Exclusions are applied before the filters. You can exclude by class or by name:
package com.example.app;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Application {
}Use excludeName when the class is not on the compile classpath, and the spring.autoconfigure.exclude property (a comma-separated list) when you want to change exclusions without recompiling. A later chapter covers exclusion in detail. For discovery, one rule matters. Boot checks that each excluded class is actually an auto-configuration candidate, and it fails with an IllegalStateException if it is not. Excluding an ordinary @Configuration class this way is an error, not a no-op.
Checking what was found
You can see the result of discovery by starting the application with the debug flag:
java -jar app.jar --debugThe conditions evaluation report lists every candidate that was evaluated. Candidates that the early filters removed appear under the negative matches, usually with a message that a class was not found. If your own auto-configuration is absent from the report entirely, the problem is in discovery rather than in a condition. The usual causes are:
- The
.importsfile is at the wrong path, or the file name has a typo. - The file is not copied into the jar. Check that it is under
src/main/resources. - The class name in the file is misspelled or has a trailing space.
- The library still uses the old
spring.factorieskey under Boot 3.x. - The class is excluded by
exclude,excludeName, orspring.autoconfigure.exclude.
Summary of the discovery rules
@SpringBootApplicationincludes@EnableAutoConfiguration, which importsAutoConfigurationImportSelector.- The selector is deferred, so it runs after the application's own configuration classes.
- Candidates come from
AutoConfiguration.importsfiles in every jar on the classpath. - Duplicates and exclusions are removed, then metadata-based filters drop classes whose requirements are not met, without loading them.
- Auto-configurations are kept out of component scanning so that the processing order stays reliable.
Exercise
A teammate ports a library from Spring Boot 2.6 to Boot 3.2. The library's auto-configuration class is annotated with @AutoConfiguration and is still registered in META-INF/spring.factories under org.springframework.boot.autoconfigure.EnableAutoConfiguration. After the upgrade, starting with --debug shows no mention of the class in the conditions report. What is the likely cause, and what is the fix?
Show answer
Boot 3.0 removed support for the EnableAutoConfiguration key in spring.factories, so the class never becomes a candidate and is never evaluated, which is why it is absent from the report. The fix is to create META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports in the library's resources and list the fully qualified class name on its own line, then remove the old spring.factories entry.
Exercise
Why does Spring Boot exclude auto-configuration classes from component scanning, and what could go wrong if a scan picked one up?
Show answer
Auto-configurations must be processed after the application's own configuration so that conditions such as @ConditionalOnMissingBean see the user's beans. A component scan would register the class as ordinary configuration, before the deferred import step. Its conditions would then run too early against an incomplete set of bean definitions, and it might register a bean that the application was about to define itself.