Sub topic 10
Controlling Auto-Configuration Order
This chapter assumes you know how @ConditionalOnBean and @ConditionalOnMissingBean work. It also assumes you know how auto-configurations are registered in AutoConfiguration.imports.
Why order matters
Conditions are evaluated while Spring registers bean definitions, not when it creates beans. If auto-configuration A has @ConditionalOnBean(DataSource.class) and is processed before the configuration that defines the DataSource, the condition fails. The failure is silent, and the bean is never created.
The same applies to @ConditionalOnMissingBean. If your auto-configuration is processed first, it can define a bean that another auto-configuration should have defined. Both definitions then compete.
Ordering controls the sequence in which configuration classes are processed. It does not control the order in which beans are instantiated. Bean creation follows the dependency graph, so injecting one bean into another already guarantees the right creation order.
The sorting rules
Boot sorts the auto-configuration candidates in three passes.
- Alphabetically by class name.
- By
@AutoConfigureOrder, where a lower value runs earlier and the default is0. - By
beforeandafterconstraints.
User-defined configuration (found by component scanning) is always processed before any auto-configuration. This is why @ConditionalOnMissingBean can detect your own beans.
Using before and after
Declare relative order on @AutoConfiguration. This example defines a bean that needs a DataSource, so it must be processed after the class that creates one.
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
@AutoConfiguration(after = DataSourceAutoConfiguration.class)
@ConditionalOnBean(DataSource.class)
public class AuditAutoConfiguration {
public record AuditWriter(DataSource dataSource) { }
@Bean
@ConditionalOnMissingBean
public AuditWriter auditWriter(DataSource dataSource) {
return new AuditWriter(dataSource);
}
}Without after = DataSourceAutoConfiguration.class, the result would depend on alphabetical order. AuditAutoConfiguration sorts before DataSourceAutoConfiguration, so the @ConditionalOnBean check would run too early and fail.
Use before when your configuration must be processed ahead of another one. It works the same way in the other direction.
Referring to classes by name
before and after take class literals. If the target class might not be on the classpath, the literal can cause a class-loading error when Boot reads the annotation. Use beforeName and afterName with strings instead.
@AutoConfiguration(afterName = "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration")
public class SafeAuditAutoConfiguration {
}Using @AutoConfigureOrder
@AutoConfigureOrder gives a configuration an absolute position. Use it sparingly, for coarse placement such as "run early".
@AutoConfiguration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
public class EarlyAutoConfiguration {
}Prefer before and after. They state a real dependency between two classes. An absolute number says nothing about why the order matters, and it can collide with numbers chosen by other libraries.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `@ConditionalOnBean` never matches, with no error | Your class runs before the one that defines the bean | Add `after` or `afterName` |
| Two beans of the same type exist | Your `@ConditionalOnMissingBean` ran before the other definition | Add `before` or `after` to fix the sequence |
| Startup fails with a circular reference between auto-configurations | `before` and `after` constraints form a cycle | Remove one constraint |
| Ordering has no effect | The class is a regular `@Configuration` found by component scanning | Register it as an auto-configuration |
Ordering attributes apply only to auto-configuration classes. Ordering a regular @Configuration with @AutoConfigureOrder does nothing, and component-scanned classes are always processed first.
Checking the result
Run the application with --debug and read the conditions report. A @ConditionalOnBean miss shows the message "did not find any beans of type". If you expected the bean to exist, ordering is the first thing to check.
Exercise
Your auto-configuration uses @ConditionalOnBean(DataSource.class), but the condition never matches even though the application has a DataSource from DataSourceAutoConfiguration. What is the likely cause, and how do you fix it?
Show answer
Your class is processed before DataSourceAutoConfiguration, so the DataSource definition is not registered when the condition runs. Add after = DataSourceAutoConfiguration.class (or afterName with the full class name) to @AutoConfiguration.