Clean • Professional
In Spring, conditional beans are beans that are created only when specific conditions are met at runtime. This allows developers to control bean registration dynamically, making applications flexible, environment-aware, and modular.
Conditional beans are useful to:
dev, test, prod).Spring provides several built-in annotations to define these conditions without writing custom logic.

@Conditional (Custom Conditions)Condition interface.Example:
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;
@Component
@Conditional(MyCondition.class)
publicclassMyBean {
publicMyBean() {
System.out.println("MyBean created!");
}
}
Custom Condition:
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
publicclassMyConditionimplementsCondition {
@Override
publicbooleanmatches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return"true".equals(System.getProperty("my.feature.enabled"));
}
}
Behavior:
MyBean is created only if the system property my.feature.enabled=true.
@ConditionalOnPropertyCreates a bean only if a specified property exists or has a specific value.
Example:
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "feature.payment.enabled", havingValue = "true")
publicclassPaymentService {
publicPaymentService() {
System.out.println("PaymentService created!");
}
}
Behavior:
Bean is created only if feature.payment.enabled=true.
@ConditionalOnClass / @ConditionalOnMissingClass@Component
@ConditionalOnClass(name = "com.example.ExternalLibrary")
publicclassExternalIntegrationService {
// Created only if ExternalLibrary is present
}
@ConditionalOnBeanBean is created only if another specific bean exists in the application context.
@Bean
@ConditionalOnBean(name = "dataSource")
public DatabaseServicedbService() {
returnnewDatabaseService();
}
@ConditionalOnMissingBeanBean is created only if no other bean of the same type exists.
@Bean
@ConditionalOnMissingBean
public MyServicedefaultService() {
returnnewMyService();
}
@ConditionalOnExpressionBean is created based on a Spring Expression Language (SpEL) expression.
@Bean
@ConditionalOnExpression("${feature.enabled:false} == true")
public FeatureServicefeatureService() {
returnnewFeatureService();
}
@ConditionalOnResourceBean is created only if a specific resource (e.g., a file) exists on the classpath.
@Bean
@ConditionalOnResource(resources = "classpath:config/feature.properties")
public ResourceBasedServiceresourceService() {
returnnewResourceBasedService();
}
@ConditionalOnWebApplication / @ConditionalOnNotWebApplicationBean is created depending on whether the application is a web application or not.
@Bean
@ConditionalOnWebApplication
public WebServicewebService() {
returnnewWebService();
}
@Bean
@ConditionalOnNotWebApplication
public ConsoleServiceconsoleService() {
returnnewConsoleService();
}
@ProfileBean is created only for a specific Spring profile (dev, test, prod).
@Component
@Profile("dev")
publicclassDevDatabaseConfig {
publicDevDatabaseConfig() {
System.out.println("Dev DB Config created!");
}
}
spring.profiles.active=dev.@Lazy for delayed initialization.@ConditionalOnClass or @ConditionalOnMissingBean for optional dependencies and fallback implementations.Conditional beans allow Spring applications to adapt dynamically to runtime environments, configuration properties, available dependencies, and classpath resources. This makes applications flexible, efficient, and modular, supporting feature toggles, optional modules, and multi-environment setups