Sub topic 5
Backing off when the user defines a bean
This chapter covers @ConditionalOnMissingBean and @ConditionalOnBean. It assumes you know Spring dependency injection, @Configuration and @Bean classes, and the @Conditional basics from the previous chapters.
Why auto-configuration must back off
An auto-configuration supplies defaults. A user who defines their own bean of the same type expects that bean to win. The mechanism is called back-off: the auto-configuration notices the user's bean and skips its own.
@ConditionalOnMissingBean implements back-off. The condition matches only when no bean of the given type is already registered in the bean factory.
package com.example.greeting;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
@AutoConfiguration
public class GreetingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public GreetingService greetingService() {
return new GreetingService("Hello");
}
}When you put the annotation on a @Bean method, the type to check defaults to the method's return type. Here, Spring looks for any existing GreetingService bean. If it finds one, the method is skipped.
You can name the type explicitly with value, check by bean name, or match on annotations. The default is usually what you want.
Why user beans are seen first
Back-off only works if the user's bean definitions are registered before the condition is evaluated. Spring Boot guarantees this by processing auto-configurations after all user configuration classes. Your @Configuration classes and component-scanned beans are registered first. Then the candidates from the AutoConfiguration.imports file are evaluated.
Because of this ordering, @ConditionalOnMissingBean is reliable in auto-configuration classes. In an ordinary user @Configuration class it is not, because the result depends on which class Spring happens to process first.
Requiring a bean with @ConditionalOnBean
@ConditionalOnBean is the opposite condition. It matches only when a bean of the given type already exists. Use it when your configuration only makes sense alongside another bean.
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);
}
}The after attribute matters here. The DataSource definition may come from DataSourceAutoConfiguration. If AuditAutoConfiguration were processed first, that definition would not exist yet, and the condition would fail. With after, the DataSource is registered before the check runs. Ordering is covered in detail in a later chapter.
Common errors
- Using
@ConditionalOnBeanin a user configuration class. The result depends on processing order, so it can pass or fail unpredictably. The Spring Boot documentation recommends these two conditions only on auto-configuration classes. - Forgetting
afterwhen depending on another auto-configuration's bean. The condition silently evaluates to false, and your bean never appears. The conditions report shows the cause. - Expecting back-off between two auto-configurations without ordering. Whichever is processed first registers its bean, and the other backs off. Set the order deliberately.
- Checking the wrong type. If the
@Beanmethod returns an interface in one place and an implementation in another, the type check may not match what the user defined. Declare the most specific return type that callers need.
Summary
@ConditionalOnMissingBeanlets a user's bean replace your default.- Auto-configurations run after user configuration, so back-off is reliable there.
@ConditionalOnBeanneeds the bean it checks for to be registered earlier, so pair it withafterwhen that bean comes from another auto-configuration.
Exercise
Your auto-configuration has @ConditionalOnBean(DataSource.class), but your bean is never created even though the application has a working DataSource from DataSourceAutoConfiguration. What is the likely cause and the fix?
Show answer
Your auto-configuration is probably processed before DataSourceAutoConfiguration, so the DataSource definition is not registered when the condition is evaluated. Fix it by adding after = DataSourceAutoConfiguration.class to @AutoConfiguration.
Exercise
Why is @ConditionalOnMissingBean reliable on an auto-configuration's @Bean method but not on a bean in a regular user @Configuration class?
Show answer
Spring Boot processes auto-configurations after all user configuration, so user bean definitions already exist when the condition runs. Among user classes there is no such guarantee, so the outcome depends on processing order.