How to Register a Spring Bean Only When Multiple Properties Are True
This article explains three ways to conditionally register a Spring Boot bean—using @ConditionalOnExpression, AllNestedConditions, and AnyNestedCondition—so that the bean is created only when specified configuration properties or other beans meet the required criteria.
Environment: Spring Boot 2.3.10
Requirement: a bean should be registered only when multiple conditions are satisfied.
Method 1: @ConditionalOnExpression
Annotation description:
<code>@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnExpressionCondition.class)
public @interface ConditionalOnExpression {
/**
* The SpEL expression to evaluate. Expression should return {@code true} if the
* condition passes or {@code false} if it fails.
* @return the SpEL expression
*/
String value() default "true";
}
</code>The SpEL expression must return true or false. Use the built‑in @ConditionalOnExpression to control bean registration.
<code>@Bean
@ConditionalOnExpression("${pack.datasource.enabled:true} and ${pack.cache.enabled:true}")
public Animal animal() {
return new Animal();
}
</code>Only when both
pack.datasource.enabledand
pack.cache.enabledare true will the Animal bean be created.
Method 2: AllNestedConditions & AnyNestedCondition
AllNestedConditions registers the bean only when all nested conditions are met.
<code>public class DataSourceAllCondition extends AllNestedConditions {
public DataSourceAllCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
public DataSourceAllCondition(ConfigurationPhase configurationPhase) {
super(configurationPhase);
}
@ConditionalOnBean(UsersController.class)
static class ExistClass { }
@ConditionalOnProperty(prefix = "pack.datasource", name = "enabled", havingValue = "true", matchIfMissing = false)
static class PropertyEnabled { }
@ConditionalOnProperty(prefix = "pack.cache", name = "enabled", havingValue = "true", matchIfMissing = false)
static class PropertyEnabled { }
}
</code> <code>@Bean
@Conditional(DataSourceAnyCondition.class)
public Animal animal() {
return new Animal();
}
</code>In this example three conditions must be satisfied (UsersController bean present,
pack.datasource.enabled=true,
pack.cache.enabled=true) for the bean to be registered.
AnyNestedCondition registers the bean when any one of the nested conditions is true.
<code>public class DataSourceAnyCondition extends AnyNestedCondition {
public DataSourceAnyCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
public DataSourceAnyCondition(ConfigurationPhase configurationPhase) {
super(configurationPhase);
}
@ConditionalOnProperty(prefix = "pack.datasource", name = "enabled", havingValue = "true", matchIfMissing = false)
static class PropertyEnabled { }
@ConditionalOnProperty(prefix = "pack.cache", name = "enabled", havingValue = "true", matchIfMissing = false)
static class PropertyEnabled { }
}
</code>Only one of the two property conditions needs to be true for the bean to be created.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.