The @Conditional annotation tells Spring to register a bean, or process a whole configuration class, only when a Condition returns true at startup. It is the foundation of conditional configuration in the Spring Framework (since Spring 4.0) and of every @ConditionalOn... annotation in Spring Boot.
You use it to switch beans on or off based on the operating system, a property, a class on the classpath, another bean, or any custom logic. That keeps one codebase flexible across environments without if statements scattered through your code.
@Conditional takes one or more classes that implement org.springframework.context.annotation.Condition. Spring evaluates them while it is reading bean definitions, before any bean is instantiated. If every condition matches, the annotated element is registered. If any condition fails, it is skipped as if it did not exist.
You can place the annotation on:
@Bean method, to make one bean conditional@Configuration or @Component class, to make the whole class (and its nested beans) conditionalThe Condition interface has a single method:
ConditionContext gives you the Environment, the BeanDefinitionRegistry, the ClassLoader and the ResourceLoader. AnnotatedTypeMetadata lets you read the annotations on the element being checked.
The example below registers a different FileStore implementation depending on the operating system.
On Linux only unixFileStore is registered, so injecting a FileStore always resolves to exactly one bean. The os.name property is a JVM system property, and Spring's Environment exposes system properties alongside environment variables and application.properties.
Placing @Conditional on a configuration class applies the check to everything inside it. If the condition fails, none of its @Bean methods, @Import entries or component scans are processed.
Use class-level conditions to group related beans behind one switch, and method-level conditions when only one bean differs.
A bare Condition class cannot take parameters. To make a condition configurable, create your own annotation, meta-annotate it with @Conditional, and read its attributes from the metadata.
Now @ConditionalOnOperatingSystem("windows") reads cleanly on any bean. This is exactly how Spring Boot builds its own annotations.
Spring Boot ships ready-made annotations, all built on @Conditional (through SpringBootCondition). Prefer these over writing your own.
| Annotation | Registers the bean when |
|---|---|
@ConditionalOnProperty | a configuration property has a given value |
@ConditionalOnClass | a class is on the classpath |
@ConditionalOnMissingClass | a class is not on the classpath |
@ConditionalOnBean | a bean of a type or name already exists |
@ConditionalOnMissingBean | no bean of that type or name exists |
@ConditionalOnWebApplication | the app is a servlet or reactive web app |
@ConditionalOnExpression | a SpEL expression evaluates to true |
@ConditionalOnResource | a resource exists |
@ConditionalOnCloudPlatform | the app runs on a given cloud platform |
Auto-configuration relies heavily on @ConditionalOnMissingBean: Boot supplies a default bean, and backs off as soon as you define your own.
@Profile is itself implemented with @Conditional (via ProfileCondition). Use @Profile("prod") when the only question is which profile is active. Use @Conditional or a @ConditionalOn... annotation when the decision depends on properties, classpath contents, other beans or custom logic.
matches. You can inspect bean definitions through the registry, but not call beans.@ConditionalOnBean depends on the order in which definitions are processed. Use it in auto-configuration classes, not in regular user configuration, where ordering is not guaranteed.matches simple and null-safe.--debug and read the condition evaluation report.@Profile checks only whether a named profile is active. @Conditional runs arbitrary logic, so it can look at properties, the classpath, existing beans or the environment. @Profile is built on top of @Conditional.
Yes. @Conditional accepts an array of classes, and all of them must match. For OR logic, extend AnyNestedCondition, and for AND logic across several nested conditions extend AllNestedConditions. Both are in org.springframework.boot.autoconfigure.condition.
Yes. @Conditional and Condition are part of spring-context since Spring 4.0. Only the @ConditionalOn... family belongs to Spring Boot.
During the configuration parsing and bean definition registration phase, before bean instances are created. That is why conditions cannot depend on the state of a running bean.
@Conditional makes bean registration depend on a Condition implementation evaluated at startup. Put it on @Bean methods or configuration classes, wrap parameterised checks in your own annotation, and prefer Spring Boot's @ConditionalOnProperty, @ConditionalOnClass and @ConditionalOnMissingBean where they fit. Use --debug to see the condition evaluation report when a bean is missing unexpectedly.