Spring Boot bean conditional on ConfigurationProperties value
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Spring Boot, creating a bean conditionally based on configuration is common, but the easiest way is usually not to inspect the @ConfigurationProperties object itself. Conditions are evaluated early during configuration, so the standard solution is to read the raw property through @ConditionalOnProperty or a custom Condition, then bind the same property into @ConfigurationProperties for normal use.
The Simplest Solution: @ConditionalOnProperty
If the condition is based on one property value, @ConditionalOnProperty is usually enough.
And the properties file:
This is the standard Spring Boot approach. Even if you also bind feature.mail.* into a configuration properties class, the condition itself can rely directly on the property key.
Using @ConfigurationProperties Alongside the Condition
You can still bind the values into a typed object for the rest of the application.
Then use it in the bean method:
This is usually cleaner than trying to make the condition depend on the already-bound object.
Why Not Condition Directly on the Properties Bean?
A common point of confusion is that @ConditionalOnProperty reads the environment, not the fully constructed @ConfigurationProperties bean. That is intentional. Conditions are evaluated while the context is deciding what bean definitions to register.
Because of that lifecycle, asking Spring to create bean A only if a bound properties bean has a certain runtime value is often the wrong level of abstraction. The environment property itself is the input that should drive the condition.
When You Need More Complex Logic
If the condition is more complicated than one property comparison, write a custom Condition.
Use it like this:
This gives you full control when @ConditionalOnProperty is too limited.
Alternative: Always Create One Bean and Make It a No-Op
Sometimes conditional bean registration is overkill. If the bean is lightweight, it can be simpler to always register it and let the implementation disable itself based on the properties.
That can reduce configuration complexity, especially when many downstream beans depend on the type. The right choice depends on whether the feature flag changes object graph shape or only behavior.
Common Pitfalls
A common mistake is expecting Spring to evaluate a condition after @ConfigurationProperties binding has already happened. Bean conditions are usually based on environment state, not on fully initialized beans.
Another issue is forcing complex logic into @ConditionalOnProperty when a custom Condition would be clearer.
Developers also sometimes forget havingValue compares strings from the environment. That matters when the property is missing or has unexpected formatting.
Finally, keep the property key stable. Conditional bean registration is much easier to reason about when one well-named property controls the feature explicitly.
Summary
- Use
@ConditionalOnPropertyfor simple property-based bean activation. - Bind the same values into
@ConfigurationPropertiesfor typed access elsewhere. - Do not expect the condition to depend on an already-created properties bean.
- Use a custom
Conditionwhen the activation rule is more complex. - Sometimes a no-op implementation is simpler than conditional bean registration.
Related reading
- Spring Boot Cannot access REST Controller on localhost 404
- Spring Boot Cannot access REST Controller on localhost 404
- Spring Boot can't autowire ConfigurationProperties
- Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup
- Spring Boot ClassNotFoundException when configuring maxUploadSize of CommonMultipartResolver
- Spring Boot classpath
- Spring Boot Cloud Zuul Proxy 404 Error
- Spring boot config server

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.