Sub topic 5
Conditional annotations that drive the decisions
This chapter assumes you know beans, @Configuration and @Bean, and have run a simple Spring Boot application. It also assumes you know that auto-configuration classes are ordinary configuration classes that Boot loads for you. Here you will see how conditions decide whether each class and each bean is used.
Why conditions exist
Auto-configuration cannot create every bean it knows about. It would fail when a library is missing, and it would clash with beans you define yourself. Boot solves this with conditional annotations. Each one is a test that Spring evaluates while it reads the configuration. If the test fails, the annotated class or @Bean method is skipped.
A condition can sit on a whole @Configuration class or on a single @Bean method. A class-level condition that fails skips every bean inside the class.
The conditions you will meet most
| Annotation | Applies when | Typical use |
|---|---|---|
| `@ConditionalOnClass` | A named class is on the classpath | Enable a feature only if its library is present |
| `@ConditionalOnMissingClass` | A named class is not on the classpath | Provide a fallback |
| `@ConditionalOnBean` | A bean of a type already exists | Add a bean that depends on another |
| `@ConditionalOnMissingBean` | No bean of a type exists | Provide a default that the user can replace |
| `@ConditionalOnProperty` | A property has a given value | Turn a feature on or off |
Classpath conditions
@ConditionalOnClass is what links starters to auto-configuration. A starter adds a library to the classpath, and the auto-configuration for that library notices it. Without the library, the condition fails and the class is skipped. The annotation reads the class from metadata, so Spring can check it without loading the class.
Bean conditions
@ConditionalOnMissingBean is what lets your code win over the defaults. Boot evaluates your own configuration first, then the auto-configurations. By then your bean is registered, so the default backs off.
Property conditions
@ConditionalOnProperty reads the environment. This example creates the bean only when greeting.enabled is true:
@Bean
@ConditionalOnProperty(prefix = "greeting", name = "enabled", havingValue = "true", matchIfMissing = true)
public GreetingService greetingService() {
return new GreetingService("Hello");
}matchIfMissing = true makes the bean active when the property is absent. Without it, a missing property means the condition fails. That default catches many people out.
Reading a real example
Here is the auto-configuration from the topic's running example:
@AutoConfiguration
@ConditionalOnClass(GreetingService.class)
@EnableConfigurationProperties(GreetingProperties.class)
public class GreetingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public GreetingService greetingService(GreetingProperties properties) {
return new GreetingService(properties.getMessage());
}
}Read it from the outside in:
@ConditionalOnClass(GreetingService.class)on the class asks whether the library is present. If not, nothing else in the class runs.@ConditionalOnMissingBeanon the method asks whether anyone has already defined aGreetingService. If so, this method is skipped.- Otherwise the bean is created with the configured message.
How evaluation order affects results
Conditions run in the order Spring processes the configuration, so ordering matters for bean conditions.
flowchart TDDo not rely on the line above. The real diagram follows.
Common errors
- Using
@ConditionalOnBeanin ordinary configuration. It checks beans registered so far, so the result depends on order. Reserve it and@ConditionalOnMissingBeanfor auto-configuration, which Boot processes after user configuration. - Referencing an optional class in a method signature. If the class is absent, loading the configuration class can fail before the condition runs. Put the condition on the class, or move the bean to a nested
@Configurationclass. - Forgetting
matchIfMissing. The feature stays off until someone sets the property. - Expecting
@ConditionalOnMissingBeanto match by name. With no arguments it matches by the method's return type.
Exercise
A bean method has @ConditionalOnProperty(prefix = "cache", name = "enabled", havingValue = "true"). The property cache.enabled is not set anywhere. Is the bean created, and how would you change that?
Show answer
The bean is not created, because a missing property fails the condition by default. Add matchIfMissing = true to make the bean active when the property is absent.
Exercise
Why does a default bean marked @ConditionalOnMissingBean in an auto-configuration back off when you define your own bean of the same type?
Show answer
Boot processes user configuration before auto-configurations. Your bean is already registered when the condition runs, so the condition finds a bean of that type and fails.