Spring ConditionalOnProperty havingValue value1 or value2
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
@ConditionalOnProperty is useful for simple exact matches, but it does not support "value1 or value2" logic through a single havingValue attribute. If one property should match either of two values, the usual solutions are @ConditionalOnExpression, a custom Condition, or restructuring the property design.
What @ConditionalOnProperty Actually Supports
A normal case looks like this:
This condition checks one property against one expected string.
What it does not do is interpret something like:
as an OR condition. That would simply compare the property to the literal string "value1,value2", which is almost never what you want.
Use @ConditionalOnExpression For Simple OR Logic
If the condition is short and readable, @ConditionalOnExpression is often the easiest answer.
This works because the expression explicitly spells out the OR condition.
It is fine for small checks, but once the expression starts getting longer, readability drops quickly.
Use A Custom Condition For Clearer Logic
For anything slightly more complex, a custom Condition is cleaner and easier to test.
Use it like this:
This approach is explicit and scales better than piling logic into a SpEL string.
Another Option: Split The Configuration
Sometimes the real problem is the property design. If you find yourself writing many value-based conditions on one mode string, the configuration may be cleaner as multiple boolean flags or a clearer enum-like setting.
For example, instead of:
you may prefer:
Then a normal @ConditionalOnProperty becomes enough:
This is often easier to understand than encoding several unrelated meanings into one string property.
When Multiple Names Help And When They Do Not
@ConditionalOnProperty does allow multiple property names:
But that means all named properties are checked against the same value rule. It does not mean one property can match several alternative values.
That distinction trips people up regularly. Multiple names are about combining several properties, not about OR-matching one property against several candidates.
Choose The Simplest Readable Option
A good rule:
- use
@ConditionalOnPropertyfor one property and one value - use
@ConditionalOnExpressionfor a small one-off OR - use a custom
Conditionwhen the logic deserves a name
This keeps configuration conditions understandable instead of turning bean registration into hidden string logic.
Common Pitfalls
The biggest mistake is assuming havingValue supports comma-separated alternatives. It does not. Spring treats that as one literal expected value.
Another mistake is reaching for SpEL immediately even when a cleaner custom condition or better property model would be easier to maintain.
People also confuse multiple property names with multiple allowed values. Those are different features with different semantics.
Finally, if the condition logic starts to describe business behavior instead of deployment configuration, it may belong in ordinary application code rather than bean registration.
Summary
- '
@ConditionalOnPropertymatches one property against one expected value.' - It does not support "value1 or value2" through a single
havingValue. - Use
@ConditionalOnExpressionfor simple OR checks. - Use a custom
Conditionwhen the logic should be explicit and reusable. - Sometimes the cleanest fix is redesigning the property itself instead of forcing more conditional syntax.
Related reading
- Spring confusion over the right learning path
- Spring controller advice does not correctly handle a CompletableFuture completed exceptionally
- Spring cron expression for every day 101am
- Spring CrudRepository findByInventoryIds(List<Long> inventoryIdList) - equivalent to IN clause
- Spring Data - ignore parameter if it has a null value
- Spring Data Elastic Search vs Java High Level REST Client
- Spring data, find by property of a nested object
- Spring Data JPA - could not initialize proxy - no Session - With Methods marked as transactional

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.