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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Register a Spring Bean Only When Multiple Properties Are True

Environment: Spring Boot 2.3.10

Requirement: a bean should be registered only when multiple conditions are satisfied.

Method 1: @ConditionalOnExpression

Annotation description:

@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";
}

The SpEL expression must return true or false. Use the built‑in @ConditionalOnExpression to control bean registration.

@Bean
@ConditionalOnExpression("${pack.datasource.enabled:true} and ${pack.cache.enabled:true}")
public Animal animal() {
    return new Animal();
}

Only when both pack.datasource.enabled and pack.cache.enabled are true will the Animal bean be created.

Method 2: AllNestedConditions & AnyNestedCondition

AllNestedConditions registers the bean only when all nested conditions are met.

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 { }
}
@Bean
@Conditional(DataSourceAnyCondition.class)
public Animal animal() {
    return new Animal();
}

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.

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 { }
}

Only one of the two property conditions needs to be true for the bean to be created.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Spring BootConditional BeanAllNestedConditionsAnyNestedCondition@ConditionalOnExpression
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.