Sub topic 7
When conditions are evaluated
This chapter assumes you know @Configuration and @Bean, and that you have seen @ConditionalOnClass and @ConditionalOnBean. Here you will learn when Spring checks each condition. That timing explains why a condition can match in one class and fail in another.
Two phases
Spring evaluates a condition at one of two points. These points are the phases of the ConfigurationCondition interface.
PARSE_CONFIGURATION: Spring checks the condition while it reads a configuration class. A failed check skips the whole class, including its nested classes and@Importtargets.REGISTER_BEAN: Spring checks the condition when it turns a configuration class into bean definitions. A failed check skips only that@Beanmethod or class.
Most conditions run in the parse phase. They only need the classpath, the environment or a resource, and none of those change while Spring processes configuration. @ConditionalOnBean and @ConditionalOnMissingBean run in the register phase, because they need to know which bean definitions already exist.
| Condition | Phase | What it inspects |
|---|---|---|
| `@ConditionalOnClass` | Parse | Classpath |
| `@ConditionalOnProperty` | Parse | Environment |
| `@ConditionalOnBean` | Register | Bean definitions registered so far |
| `@ConditionalOnMissingBean` | Register | Bean definitions registered so far |
The order of evaluation
Spring Boot processes configuration in this sequence:
- Spring reads your own
@Configurationclasses and component-scanned classes and registers their beans. - Spring Boot reads the candidates from
AutoConfiguration.imports. It drops any that fail the@ConditionalOnClasscheck, which it can often make without loading the class. - Spring sorts the remaining auto-configurations by their
beforeandafterattributes. It then processes them one at a time. Each class registers its bean definitions before the next class starts.
Auto-configurations come after user configuration because Spring Boot imports them through a deferred import selector. That is why @ConditionalOnMissingBean can back off from a bean you defined. Your definition is already registered when the auto-configuration is checked.
Definitions, not instances
Bean conditions look at bean definitions, not at created objects. Spring has not instantiated any bean when it evaluates them. A condition only sees what has been registered at that moment. It cannot see definitions that a later class will register.
Why order matters for @ConditionalOnBean
Suppose an auto-configuration needs a DataSource. It should only apply if one exists:
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);
}
}after = DataSourceAutoConfiguration.class makes Spring process the data source class first. Its DataSource definition is then registered when @ConditionalOnBean(DataSource.class) runs. Without after, the check could run first. It would find no DataSource, and AuditAutoConfiguration would be skipped. The failure is silent, and it can depend on the sort order. A change on the classpath could make it appear or disappear.
Common mistakes
- Using
@ConditionalOnBeanwithoutafter. The condition then depends on whichever order Spring happens to choose. - Expecting a bean condition to see a bean that a later auto-configuration registers.
- Putting
@ConditionalOnBeanon a user@Configurationclass and expecting it to see auto-configured beans. Auto-configurations run after user configuration. - Checking for a class on
@Beanmethods when you meant to guard the whole class. A class-level@ConditionalOnClassstops Spring from loading the class that refers to the missing type. A method-level one can fail when Spring reads the class.
Exercise
An auto-configuration class is annotated with @ConditionalOnBean(DataSource.class) but has no after attribute. It sometimes does not apply even though the application has a data source. Why, and how do you fix it?
Show answer
@ConditionalOnBean is checked in the register phase against the bean definitions registered so far. If the class is processed before DataSourceAutoConfiguration, the DataSource definition does not exist yet and the condition fails. Add after = DataSourceAutoConfiguration.class to @AutoConfiguration so the data source definition is registered first.