How to check two condition while using ConditionalOnProperty or ConditionalOnExpression
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Spring Boot often needs more than one property check before creating a bean. The right annotation depends on the shape of the rule: use @ConditionalOnProperty for straightforward property matching, @ConditionalOnExpression for simple boolean combinations, and a custom Condition when the logic becomes complex enough that annotations start hiding the real behavior.
@ConditionalOnProperty Handles Simple AND Checks
When several properties must all be present and all match the same expected value, @ConditionalOnProperty is the clearest option.
This condition matches only when both app.feature.enabled=true and app.feature.audit=true.
Use matchIfMissing Deliberately
By default, @ConditionalOnProperty requires the property to exist. If missing properties should behave as enabled, you can change that explicitly.
This is powerful, but it changes startup behavior in a way that is easy to forget later. Use it only when the default should really be opt-in by omission.
@ConditionalOnExpression for Mixed Boolean Logic
If your rule is more like "enabled and one of two modes is active," @ConditionalOnExpression is usually the simplest annotation-based choice.
This works, but it is less readable than @ConditionalOnProperty. SpEL expressions also become harder to debug as the rule grows.
When a Custom Condition Is Better
Once the condition involves multiple branches, parsing, or reusable logic, write a custom Condition. That keeps the rule in normal Java code instead of embedding it into an annotation string.
Then apply it like this:
This is more verbose, but it is also easier to test and maintain.
A Simple Decision Rule
Use this mental model:
- same expected value across several properties:
@ConditionalOnProperty - short boolean expression across a few properties:
@ConditionalOnExpression - anything business-specific or complex: custom
Condition
This keeps your configuration readable at startup time, which matters because conditional bean logic can be hard to trace once a project grows.
Common Pitfalls
The most common mistake is reaching for @ConditionalOnExpression too early. A short SpEL string looks compact, but it becomes hard to read and debug faster than most teams expect.
Another issue is forgetting that @ConditionalOnProperty with multiple name values behaves like an AND check for the same havingValue. It is not a general-purpose boolean expression language.
Developers also trip over quoting and default values inside SpEL placeholders. If a property is missing and you did not provide a safe default, the expression may not behave the way you think.
Finally, keep conditions about configuration, not application state. These annotations are for bean registration at startup, not for runtime feature toggling after the application is already running.
Summary
- Use
@ConditionalOnPropertyfor simple property presence or same-value AND checks. - Use
@ConditionalOnExpressionfor short mixed boolean logic. - Use
matchIfMissingonly when a missing property should intentionally count as a match. - Write a custom
Conditionwhen the rule stops being readable as an annotation. - Keep startup conditions explicit so bean creation remains easy to reason about.

