@ConditionalOnProperty in Spring Boot
`@ConditionalOnProperty` lets Spring Boot create a configuration or bean only when a specified application property satisfies the condition. It is commonly used in auto-configuration to make features configurable without requiring custom Java configuration.
`@ConditionalOnProperty` is a Spring Boot condition annotation used to control whether a configuration class or bean definition is applied. It typically checks a property from the Spring `Environment`, such as `feature.enabled`.
The annotation can specify `prefix`, `name` (or `value`), and `havingValue`. For example, `@ConditionalOnProperty(prefix = "feature", name = "enabled", havingValue = "true")` activates the configuration when `feature.enabled=true`.
If `havingValue` is omitted, the condition generally matches when the property exists and its value is not `false`. `matchIfMissing = true` can make the condition match when the property is absent; otherwise, a missing property normally prevents a match.
This mechanism is especially useful in Spring Boot auto-configuration because libraries can provide optional beans while allowing application properties to turn those beans on or off. The condition is evaluated during configuration processing, before the dependent bean is normally created.
Key points
- Use `@ConditionalOnProperty` to make configuration conditional on an application property.
- Use `prefix` and `name` to construct the property key, such as `feature.enabled`.
- Use `havingValue` when a specific property value should activate the condition.
- Use `matchIfMissing` to define behavior when the property is absent.
- It is commonly used by Spring Boot auto-configuration to make optional features configurable.
Interview questions
What is `matchIfMissing` used for?
`matchIfMissing` controls what happens when the selected property is absent. Its default behavior is not to match a missing property. Setting `matchIfMissing=true` makes the condition match even when the property is not defined. This is useful when a feature should be enabled by default unless an explicit property disables or changes it.
What does `@ConditionalOnProperty` do?
It tells Spring Boot to apply a configuration only when a specified application property meets the condition. The property is read from Spring's `Environment`. A common use is enabling an optional feature with a property such as `feature.enabled=true`. If the condition does not match, the associated configuration is not applied.
How do `prefix`, `name`, and `havingValue` work together?
`prefix` provides the common beginning of the property key, while `name` identifies the property itself. For example, `prefix="feature"` and `name="enabled"` target `feature.enabled`. `havingValue="true"` requires that property to have the specified value for the condition to match.
Why is `@ConditionalOnProperty` important in Spring Boot auto-configuration?
Auto-configuration needs a way to provide useful defaults without forcing every application to use every feature. `@ConditionalOnProperty` lets an auto-configuration activate only when an application setting indicates that a feature should be enabled. This keeps optional integrations and features configurable through externalized application settings. It also avoids requiring application code to manually define every conditional configuration.
What is a common mistake when using `@ConditionalOnProperty`?
A common mistake is misunderstanding the behavior of missing properties or assuming that any nonempty value will necessarily satisfy an explicitly configured `havingValue`. Another issue is constructing the property key incorrectly through `prefix` and `name`. It is also important to remember that this is configuration-time condition evaluation, not a runtime branch inside a method. For complex conditions that cannot be expressed cleanly with a property, another conditional mechanism may be more appropriate.