@Conditional Annotation in Spring
Fast recall
Quick revision
Core answer
Understanding of conditional bean registration and how Spring Boot auto-configuration decides which beans to create
Thinking @Conditional runs at every bean lookup instead of once at application startup during context initialization
Explain how @ConditionalOnClass checks for library presence on classpath and mention that this is how Spring Boot detects if you have H2 or MongoDB and auto-configures the right data source
Interview-ready guide
Interview answer
An optional cache configuration can use @ConditionalOnClass for the cache library and @ConditionalOnMissingBean for the application override. A property switch can use @ConditionalOnProperty with an explicit havingValue. If the condition does not match, its bean definition is not registered. Changing the property after startup normally does not add the bean; rebuild/refresh behavior requires separate infrastructure.
@Conditional Annotation in Spring
Direct Answer
@Conditional registers a configuration class or @Bean only when its Condition matches while the context is being built. Spring Boot provides focused conditions such as @ConditionalOnClass, @ConditionalOnMissingBean, and @ConditionalOnProperty. These are startup/configuration-time decisions for the ApplicationContext, not live per-request checks.
Code Example
// Custom condition checking if running in production
class ProductionCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
var env = context.getEnvironment();
return "production".equals(env.getProperty("spring.profiles.active"));
}
}
@Configuration
class CacheConfiguration {
@Bean
@Conditional(ProductionCondition.class) // Only in production
public CacheManager redisCacheManager() {
return new RedisCacheManager();
}
@Bean
@ConditionalOnMissingBean(CacheManager.class) // Fallback if no other CacheManager
public CacheManager simpleCacheManager() {
return new SimpleCacheManager();
}
@Bean
@ConditionalOnClass(name = "com.fasterxml.jackson.databind.ObjectMapper") // If Jackson on classpath
@ConditionalOnProperty(name = "app.json.enabled", havingValue = "true") // AND property is true
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}
Common Mistakes & Pitfalls
- Condition Evaluation Timing: Freshers think @Conditional checks happen every time you @Autowire the bean. Actually, the condition runs once during application startup when Spring builds the context. If matches returns false, the bean is never created at all.
- Public Condition Classes: Writing the Condition implementation as a private inner class fails because Spring can't instantiate it. Always make your Condition class public and ensure it has a no-arg constructor.
- Overusing Custom Conditions: Creating complex custom Condition classes when Spring Boot already provides @ConditionalOnProperty or @ConditionalOnBean. Check the 20+ built-in @Conditional variants before writing your own — @ConditionalOnWebApplication, @ConditionalOnExpression, @ConditionalOnResource all solve common cases.
Check your understanding
Answer these without looking back at the article.
- How would you write a custom @Conditional condition?
- What is @ConditionalOnProperty used for?
- How does @ConditionalOnMissingBean enable auto-configuration overriding?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.