Quick revision
The short answer
@ConditionalOnBean and @ConditionalOnMissingBean look only at the bean definitions registered so far, not at the final set of beans. Spring Boot processes all user configuration before any auto-configuration, so inside an auto-configuration class every user bean is already known and the answer is reliable. In an ordinary configuration class the answer depends on the order in which classes are processed, which is not guaranteed; auto-configurations themselves declare their relative order with @AutoConfiguration(before = ..., after = ...).
What to remember
In this answer, we saw that @ConditionalOnBean and @ConditionalOnMissingBean check the bean definitions registered so far. Ordinary configuration classes are processed in an order that is not guaranteed, so the answer can change with unrelated details. Spring Boot imports auto-configurations through a deferred import selector after all user configuration, so in auto-configuration every user bean is already known, and auto-configurations order themselves with @AutoConfiguration(before, after). That is why the documentation limits these conditions to auto-configuration classes.
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.
@ConditionalOnBean and @ConditionalOnMissingBean look only at the bean definitions registered so far, not at the final set of beans. Spring Boot processes all user configuration before any auto-configuration, so inside an auto-configuration class every user bean is already known and the answer is reliable. In an ordinary configuration class the answer depends on the order in which classes are processed, which is not guaranteed; auto-configurations themselves declare their relative order with @AutoConfiguration(before = ..., after = ...).
Why this question is asked
This sentence from the Spring Boot documentation surprises many developers, because the annotations work fine in most small examples and then fail in a larger application, or after an unrelated class is renamed. Interviewers ask about it because the answer requires understanding how Spring reads configuration in phases and why Boot imports its auto-configurations last. In this answer, we will recap how bean conditions are evaluated, reproduce the order problem in ordinary configuration, see how Spring Boot defers auto-configuration, look at ordering between auto-configurations, and finish with a few practical rules.
1A Quick Recap of Bean Conditions
When a Spring application starts, it does not create beans straight away. It first reads all configuration classes and registers bean definitions, recipes that describe each bean, in a registry. Only after that phase does it create the beans. Conditions such as @ConditionalOnMissingBean are evaluated during the first phase, when Spring is about to register a definition.
So when @ConditionalOnMissingBean(DataSource.class) asks whether a DataSource exists, the real question it answers is whether a DataSource definition has been registered at this moment. A definition that will be registered later, by a class processed afterwards, is invisible to it.
2Reproducing the Order Problem
Here are two ordinary configuration classes found by component scanning. One provides a default clock if none exists; the other defines a custom clock.
7 messages in order. Play them, or step through them one at a time.
The condition was answered before the user's bean was registered.
Which class is processed first depends on details such as the order in which component scanning finds the classes and the order of @Import declarations. Those details are not a documented contract, so renaming a package can flip the result. Notice that nothing is wrong with each class on its own; the problem is only the relative order.
3How Spring Boot Defers Auto-Configuration
Spring Boot avoids this for its own conditional beans by making sure they are processed last. @EnableAutoConfiguration, part of @SpringBootApplication, imports auto-configurations through a DeferredImportSelector. A deferred import selector is a Spring Framework feature that runs only after all other configuration classes, those found by component scanning, imported with @Import and defined by the application, have been processed.
So by the time any auto-configuration evaluates @ConditionalOnMissingBean(ObjectMapper.class), every ObjectMapper the application defines is already registered. The condition then gives the right answer every time: if the user defined one, Boot backs off; if not, Boot provides its default. This is the contract the documentation refers to.
- Process user configurationcomponent scan,
@Import,@Beanmethods - All user bean definitions registeredthe registry now knows every user bean
- Process deferred auto-configurationslisted in
AutoConfiguration.imports - Evaluate @ConditionalOnMissingBeanreliable: sees every user bean
- Create beansonly now are instances made
5 steps in order. Play them, or step through them one at a time.
4Ordering Between Auto-Configurations
Among auto-configurations, the same problem could appear again: one auto-configuration may want to back off if another one defined a bean. Spring Boot solves this with explicit ordering. @AutoConfiguration has before and after attributes, and @AutoConfigureOrder sets a numeric order. Boot sorts the auto-configurations by these declarations before processing them.
Without after, this auto-configuration might be processed before the DataSource auto-configuration and decide that no DataSource exists. With it, the order is guaranteed, and the condition can rely on it.
5Practical Rules
The first rule follows directly: use @ConditionalOnBean and @ConditionalOnMissingBean in auto-configuration classes, registered in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports, and not in classes picked up by component scanning. If an application needs a default that users can replace, it should be an auto-configuration, even inside the application's own codebase.
The second rule is about types. The condition matches on the type of the bean definition, which for a @Bean method is the method's declared return type. A method declared to return Object, or an interface narrower than the type the condition looks for, may not match. Declaring the most specific return type helps conditions and injection alike.
The third rule is to make sure auto-configuration classes are never component scanned, for example because they sit in a package below the application class. Then they are processed as ordinary configuration, too early, and the guarantee is lost.
6Follow-up questions
Why do these conditions check definitions instead of real beans?
Because the decision must be made before beans are created; otherwise Spring would create a bean only to find it should not exist, and creating beans early can trigger their dependencies prematurely. Definitions and their types are enough to decide, and they are available at that point.
Is @ConditionalOnProperty also order-dependent?
No. Property conditions look at the Environment, which is fully prepared before any configuration class is processed. Only conditions that look at other bean definitions depend on processing order.
How can I see whether a @ConditionalOnMissingBean matched?
Start the application with --debug or debug=true and read the condition evaluation report, or call the actuator conditions endpoint. Each entry says which condition matched or did not and why, for example that a bean of the type was found.
7Conclusion
In this answer, we saw that @ConditionalOnBean and @ConditionalOnMissingBean check the bean definitions registered so far. Ordinary configuration classes are processed in an order that is not guaranteed, so the answer can change with unrelated details. Spring Boot imports auto-configurations through a deferred import selector after all user configuration, so in auto-configuration every user bean is already known, and auto-configurations order themselves with @AutoConfiguration(before, after). That is why the documentation limits these conditions to auto-configuration classes.
Open with
These conditions are evaluated while Spring registers bean definitions, before any bean is created, and they only see the definitions registered up to that moment. In ordinary configuration classes, the processing order depends on component scanning and import order, which is not a documented contract.
Then, if they want more
So a ConditionalOnMissingBean default in one class can be evaluated before the user's own bean in another class is registered, and you end up with two beans and a NoUniqueBeanDefinitionException, or the reverse, depending on something as random as a package name. Spring Boot avoids this by importing auto-configurations through a DeferredImportSelector, which runs after all user configuration is processed.
By the time an auto-configuration asks whether an ObjectMapper exists, every user-defined ObjectMapper is already registered, so the back-off is reliable. Between auto-configurations, the order is made explicit with the before and after attributes of AutoConfiguration, or AutoConfigureOrder, so one auto-configuration can safely depend on another's beans.
In practice I put these conditions only in classes registered in the AutoConfiguration.imports file, make sure those classes are not component scanned, and declare specific return types on Bean methods, because the condition matches on the definition's type.
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 Framework reference, Core technologies
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 "Why does the Spring Boot documentation say @ConditionalOnBean and...": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/95e9c8b9c5/