Quick revision
The short answer
@ConditionalOnClass checks whether a class is on the classpath, @ConditionalOnMissingBean checks that no bean of a type already exists, @ConditionalOnWebApplication limits a configuration to servlet or reactive web applications, and @ConditionalOnExpression evaluates a SpEL expression. In a library I would avoid @ConditionalOnExpression: it is flexible but hard to read, hard to test and report on, and it resolves more of the environment and possibly beans early. Specific conditions and @ConditionalOnProperty are clearer, and a truly custom rule belongs in a class that extends SpringBootCondition, which gives readable report messages.
What to remember
In this answer, we compared four conditions: @ConditionalOnClass reacts to the classpath, @ConditionalOnMissingBean backs off for user beans, @ConditionalOnWebApplication limits beans to the right kind of application, and @ConditionalOnExpression runs SpEL. Libraries should avoid the last one because it is opaque, reports poorly and evaluates early, and should use @ConditionalOnProperty, specific conditions, or a SpringBootCondition subclass with clear messages instead.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
@ConditionalOnClass checks whether a class is on the classpath, @ConditionalOnMissingBean checks that no bean of a type already exists, @ConditionalOnWebApplication limits a configuration to servlet or reactive web applications, and @ConditionalOnExpression evaluates a SpEL expression. In a library I would avoid @ConditionalOnExpression: it is flexible but hard to read, hard to test and report on, and it resolves more of the environment and possibly beans early. Specific conditions and @ConditionalOnProperty are clearer, and a truly custom rule belongs in a class that extends SpringBootCondition, which gives readable report messages.
Why this question is asked
Conditions are how a library decides, quietly, what to add to an application. When they are well chosen, the library feels like part of Boot; when they are not, teams get beans they did not expect, or miss beans they did, and have to read the library's source to find out why. Interviewers ask this comparison to see whether we know what each condition checks and whether we think about the people who will debug our library later. In this answer, we will go through the four conditions with an example of each, see why @ConditionalOnExpression is the one to avoid in libraries, and write a custom condition the right way.
1@ConditionalOnClass: Is the Library There?
@ConditionalOnClass matches when the named classes can be found on the classpath. It is how auto-configuration reacts to dependencies: add the Redis client and the Redis auto-configuration activates. Spring reads the annotation from the class file, so at class level a missing class is handled cleanly; on @Bean methods or when in doubt, the name attribute with a string is safer, or a nested configuration class guarded by the condition.
Its sibling @ConditionalOnMissingClass does the opposite, for fallbacks when a library is absent.
2@ConditionalOnMissingBean: Has the User Already Defined It?
@ConditionalOnMissingBean matches when the bean factory has no bean of the given type (or name) registered so far. It is the back-off mechanism: our default bean steps aside when the application provides its own. On a @Bean method with no attributes, the type is taken from the method's return type.
Notice registered so far. This condition depends on order, and it is reliable in auto-configurations because Boot processes them after all user configuration. In a regular @Configuration class its result depends on the order classes happen to be processed in, so it should not be used there.
3@ConditionalOnWebApplication: Which Kind of Application?
@ConditionalOnWebApplication matches when the application is a web application, and its type attribute narrows it to SERVLET (Spring MVC on Tomcat or Jetty) or REACTIVE (WebFlux). A library that registers a servlet filter must not try to do it in a batch job or a WebFlux service, and this condition expresses that directly. @ConditionalOnNotWebApplication is the opposite.
4@ConditionalOnExpression and Why Libraries Should Avoid It
@ConditionalOnExpression evaluates a SpEL expression, Spring's expression language, and matches if the result is true. It can combine properties, call methods and even reference beans, which makes it tempting for complex rules.
| Condition | Checks | Use in a library |
|---|---|---|
@ConditionalOnClass | classpath, by class or name | yes: react to dependencies |
@ConditionalOnMissingBean | no bean of the type yet | yes: back off for user beans |
@ConditionalOnWebApplication | servlet or reactive web app | yes: web-only beans |
@ConditionalOnExpression | arbitrary SpEL | avoid: opaque, early evaluation |
@ConditionalOnProperty | a property value | yes: explicit feature flags |
The problems show up for the people who use the library. The rule is hidden inside a string that the IDE cannot check, so a typo is found at runtime. The condition report says only that the expression was false, not which part failed. The expression is evaluated during configuration parsing, so placeholder resolution happens early, and referencing a bean, as @auditPolicy does here, forces that bean to be created very early, before post-processors such as configuration property binding have run, which can give it the wrong settings. Comparing spring.profiles.active as a string is also wrong when several profiles are active.
Each part of the rule has a specific, readable condition instead. The property becomes @ConditionalOnProperty or, in Boot 3.5 and later, @ConditionalOnBooleanProperty; the profile check becomes @Profile("!test"); and the bean-based policy becomes a property the policy bean reads at runtime, not a startup condition.
5Writing a Custom Condition the Right Way
Sometimes no built-in condition fits, for example a rule that a feature is on when either of two properties is set. A custom condition can implement Spring's Condition interface directly, but extending Boot's SpringBootCondition is better: it returns a ConditionOutcome with a message, which appears in the condition evaluation report, and it adds useful error handling.
Notice that the report message names exactly what to set. That one line saves every consuming team from opening the library's source. For combinations of existing conditions, Boot also offers AnyNestedCondition, AllNestedConditions and NoneNestedConditions, which compose conditions declaratively and still report each part.
6Follow-up questions
What is the difference between @ConditionalOnBean and @ConditionalOnMissingBean regarding order?
Both look only at bean definitions registered so far, so both depend on processing order. In auto-configurations we control that with @AutoConfiguration(after = ...), naming the auto-configuration that defines the bean we check.
Is @ConditionalOnExpression ever acceptable?
In an application's own configuration, for a short property-based rule that one team owns, it can be fine. In a shared library, specific conditions are almost always better because consumers debug them through the report.
Can conditions be stacked?
Yes. Several condition annotations on one class or method must all match. This is the usual way to say, for example, a servlet web application with Micrometer on the classpath and no user-defined bean.
7Conclusion
In this answer, we compared four conditions: @ConditionalOnClass reacts to the classpath, @ConditionalOnMissingBean backs off for user beans, @ConditionalOnWebApplication limits beans to the right kind of application, and @ConditionalOnExpression runs SpEL. Libraries should avoid the last one because it is opaque, reports poorly and evaluates early, and should use @ConditionalOnProperty, specific conditions, or a SpringBootCondition subclass with clear messages instead.
Open with
@ConditionalOnClass matches when a class is on the classpath, so auto-configuration can react to dependencies; at class level it's read from bytecode, and I use the name attribute where a missing class could otherwise break class loading. @ConditionalOnMissingBean matches when no bean of that type has been registered yet, which is how a library's defaults back off for the user's own beans; it depends on order, so it's reliable in auto-configurations, which run after user configuration, but not in regular configuration classes. @ConditionalOnWebApplication restricts beans to web applications, and its type narrows it to servlet or reactive, which matters for things like servlet filters. @ConditionalOnExpression evaluates SpEL, and that's the one I'd avoid in a library.
Then, if they want more
The rule hides in a string the IDE can't check, the condition report only says the expression was false without saying which part, placeholders are resolved early, and referencing a bean in the expression creates that bean very early, before post-processing like property binding.
Instead I'd use specific conditions: @ConditionalOnProperty, or @ConditionalOnBooleanProperty in Boot 3.5, for flags, @Profile for profiles, and for anything truly custom, a class that extends SpringBootCondition and returns a ConditionOutcome with a message, so the report tells consuming teams exactly what to set.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare @ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnWebApplication and...": https://scrivio-interview-library.netlify.app/spring-boot/auto-configuration/2f76862e6b/