@Conditional and Custom Conditions
Fast recall
Quick revision
Core answer
true. Spring Boot's auto-configuration is built almost entirely on @Conditional variants like `@ConditionalOnClass`, `@ConditionalOnMissingBean`, and `@ConditionalOnProperty`. The Condition interface has one method — matches(ConditionContext, AnnotatedTypeMetadata) — that returns a boolean. Evaluation happens at bean definition time, before any bean is instantiated, making it the earliest decision point in the Spring lifecycle. @ConditionalOnMissingBean is the mechanism that makes Spring Boot auto-configuration override-friendly: user-defined beans are processed first, so they win over auto-configured defaults.Understanding of Spring's conditional bean registration model, which is the foundation of Spring Boot auto-configuration.
Not knowing the difference between @Profile (environment-based) and @Conditional (arbitrary logic), or not realizing that @ConditionalOnMissingBean is how Spring Boot lets you override auto-configured beans.
Explain the Condition interface's ConditionContext, show how to write a condition that checks an environment property, and describe the ordering issue: conditions are evaluated during the BeanDefinitionRegistry phase, before any beans are created.
Interview-ready guide
Interview answer
Every time Spring Boot automatically sets up a DataSource because H2 is on the classpath, or skips configuring JdbcTemplate because you already defined one, the mechanism is @Conditional. It is the decision engine behind all of Spring Boot's auto-configuration.
The core interface
Conditionhas one method:matches(ConditionContext, AnnotatedTypeMetadata)returnstrueorfalse- If
false, Spring skips the annotated@Beanor@Configurationentirely — no bean definition, no instantiation, no error - Evaluation happens at bean definition time, before any bean is created — not at runtime
The built-in conditions
@ConditionalOnClass— checks if a class is on the classpath; how Spring Boot knows to configure JPA only when Hibernate is present@ConditionalOnMissingBean— registers a bean only if no bean of that type already exists; user configurations are processed before auto-configurations, so your custom bean wins@ConditionalOnProperty— evaluates a property value at startup
Custom conditions
- Implement
Condition, useConditionContextto check theEnvironment,ClassLoader,BeanDefinitionRegistry, orResourceLoader - Compose into a custom annotation:
@ConditionalOnFeature("dark-mode")backed by a condition that readsfeature.dark-mode.enabled
@Profile is just @Conditional with a built-in condition that checks active profiles. Anything you want to conditionally enable — a feature flag, an environment-specific service, a paid-tier bean graph — can be driven by a custom @Conditional.
@Conditional and Custom Conditions
Direct Answer
@Conditional tells the container to register a bean only when a given Condition implementation returns true. Spring Boot's auto-configuration is built almost entirely on @Conditional variants like @ConditionalOnClass, @ConditionalOnMissingBean, and @ConditionalOnProperty. The Condition interface has one method — matches(ConditionContext, AnnotatedTypeMetadata) — that returns a boolean. Evaluation happens at bean definition time, before any bean is instantiated, making it the earliest decision point in the Spring lifecycle. @ConditionalOnMissingBean is the mechanism that makes Spring Boot auto-configuration override-friendly: user-defined beans are processed first, so they win over auto-configured defaults.
Common Mistakes & Pitfalls
Not knowing the difference between @Profile (environment-based) and @Conditional (arbitrary logic), or not realizing that @ConditionalOnMissingBean is how Spring Boot lets you override auto-configured beans.
Check your understanding
Answer these without looking back at the article.
- How does @ConditionalOnMissingBean let you override Spring Boot auto-configuration?
- What is the difference between @Conditional and @Profile?
- Why can't you call context.getBean() inside a Condition.matches() implementation?
- How would you write a custom @ConditionalOnFeature annotation for feature flags?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.