Sub topic 8
Controlling auto-configuration order
This chapter assumes you know @Configuration and @Bean, and that you have read the earlier chapters on @ConditionalOnBean and how AutoConfiguration.imports lists candidates.
Why order matters
Spring Boot registers bean definitions from auto-configuration classes one class at a time. A condition such as @ConditionalOnBean only sees the definitions registered before it runs. If the class that defines the bean you are checking for has not been processed yet, the condition fails, even though the bean would exist in the final context.
Ordering is therefore about what conditions can see. It does not change when beans are instantiated. Instantiation order follows dependencies, not auto-configuration order.
Ordering with `after` and `before`
@AutoConfiguration accepts after and before attributes that take class references. This class needs a DataSource definition to be visible when its condition runs:
package com.example.audit;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
@AutoConfiguration(after = DataSourceAutoConfiguration.class)
@ConditionalOnBean(DataSource.class)
public class AuditAutoConfiguration {
@Bean
public AuditLogger auditLogger(DataSource dataSource) {
return new AuditLogger(dataSource);
}
}Because DataSourceAutoConfiguration is processed first, its DataSource definition is already registered when @ConditionalOnBean(DataSource.class) is evaluated. Without after, the result would depend on the default order, and the condition could fail silently.
Use before for the opposite case: your class must register its definitions ahead of another auto-configuration, for example so that the other class's @ConditionalOnMissingBean sees yours and backs off.
Referencing classes by name
A class reference in after forces that class to be loadable. If the other auto-configuration lives in an optional library that may be absent, loading your annotation fails. Use the name-based attributes instead:
@AutoConfiguration(afterName = "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration")beforeName works the same way. A name that does not match any class on the classpath is ignored, which is the behavior you want for optional dependencies.
Coarse ordering with `@AutoConfigureOrder`
@AutoConfigureOrder sets a numeric precedence, with a default of 0. Lower values are processed earlier. Use it sparingly, for example @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) for infrastructure that everything else depends on.
Spring Boot sorts candidates in three passes:
- Alphabetically by class name, so the result is deterministic.
- By
@AutoConfigureOrdervalue. - By
beforeandafterconstraints.
The before and after constraints are applied last, so they win over a conflicting numeric order. Prefer them whenever you depend on one specific class.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `@ConditionalOnBean` never matches | The defining class is processed later | Add `after` or `afterName` |
| `ClassNotFoundException` when scanning your auto-configuration | `after` references an optional class | Use `afterName` |
| Startup fails with a circular reference error between auto-configurations | A cycle in `before` and `after` | Remove one constraint |
| Ordering attributes have no effect on a class in your app | The class is a regular `@Configuration`, not an auto-configuration | Move it into `AutoConfiguration.imports` |
Ordering applies only among auto-configurations. Your own @Configuration classes found by component scanning are always processed before every auto-configuration. This is why @ConditionalOnBean is safe for user-defined beans but fragile for beans from other auto-configurations.
Checking the result
Run the application with --debug and read the conditions report. If AuditAutoConfiguration appears under negative matches with "did not find any beans of type DataSource", the ordering is wrong or the data source auto-configuration did not apply.
Exercise
Your auto-configuration uses @ConditionalOnBean(DataSource.class) but never applies, although the application has a working DataSource from DataSourceAutoConfiguration. What do you change, and why?
Show answer
Add after = DataSourceAutoConfiguration.class (or afterName if the class may be absent) to @AutoConfiguration. Your class was processed before DataSourceAutoConfiguration registered its DataSource definition, so the condition found no matching bean. With after, the definition is registered first and the condition sees it.