@ConditionalOnProperty is a Spring Boot annotation that registers a bean, or a whole configuration class, only when a property in the Environment (for example in application.properties or application.yml) is present and has an expected value. It is the standard way to build feature toggles in Spring Boot without changing code.
The annotation lives in org.springframework.boot.autoconfigure.condition and is built on Spring's @Conditional mechanism.
| Attribute | Purpose | Default |
|---|---|---|
prefix | Common prefix for the property names (a trailing dot is added for you) | none |
name (or value) | Property name or names, appended to the prefix | none |
havingValue | Value the property must equal | not set |
matchIfMissing | Whether the condition matches when the property is absent | false |
The matching rules are:
havingValue is not set, the condition matches when the property exists and is not equal to false (case-insensitive).havingValue is set, the property must equal it (case-insensitive comparison).matchIfMissing = true.Define a bean that is created only when feature.notifications.enabled=true.
The equivalent application.yml is:
With the property set to true, NotificationService is registered. Set it to false, or remove it, and the bean does not exist, so any code that injects it must use ObjectProvider<NotificationService> or @Autowired(required = false) (or Optional).
For features that should be on by default, flip the logic with matchIfMissing = true. The bean is then created unless someone explicitly sets the property to something else.
| Property value | Bean created? |
|---|---|
app.cache.enabled=true | yes |
app.cache.enabled=false | no |
| property absent | yes (because of matchIfMissing) |
When several beans share one switch, put the annotation on the class.
If the property does not match, neither bean is registered. The same works on @Component and @Service classes.
You can pair two beans with opposite conditions to pick an implementation from configuration.
Exactly one Storage bean exists at runtime, and local storage is the default when storage.type is not set. Environment variables work too: STORAGE_TYPE=s3 is matched through Spring Boot's relaxed binding.
Use ApplicationContextRunner to test both outcomes without starting a full application.
When a bean is unexpectedly missing, start the application with --debug (or debug=true) to print the condition evaluation report, which lists every matched and unmatched condition with a reason.
havingValue is compared as text, so havingValue = "true" matches TRUE too.@Value and expecting the bean to exist when it is false.@RefreshScope or a feature flag library.By default the condition does not match and the bean is not created. Set matchIfMissing = true to create the bean when the property is absent.
No. It is part of Spring Boot (spring-boot-autoconfigure). In plain Spring, write a custom Condition or use @Profile.
Yes. Pass an array to name, for example name = {"a.enabled", "b.enabled"}. All listed properties must satisfy the condition. For OR logic use a custom AnyNestedCondition.
No. havingValue is compared ignoring case, but the property name follows Spring Boot's relaxed binding rules for names.
@ConditionalOnProperty registers a bean or configuration only when a property matches. Use prefix and name to select the property, havingValue for the expected value, and matchIfMissing to decide the default. Evaluation happens at startup, so test both outcomes with ApplicationContextRunner and use the --debug report to diagnose missing beans.