Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What does the @Conditional annotation do, and name four common Spring Boot conditions.

@Conditional(SomeCondition.class) tells Spring to register a bean or a whole configuration class only if the condition's matches method returns true. Spring Boot builds many ready-made conditions on it: @ConditionalOnClass and @ConditionalOnMissingClass check the classpath, @ConditionalOnBean and @ConditionalOnMissingBean check other bean definitions, and @ConditionalOnProperty checks a property value. Others include @ConditionalOnWebApplication and @ConditionalOnResource.

Why this question is asked

Spring Boot's auto-configuration feels like magic: add a dependency and a configured bean appears; define your own bean and Boot's version steps aside. The mechanism behind that is @Conditional, and understanding it explains most of Boot's behaviour and how to extend it. Interviewers ask this to check that we know what a condition is, when it is evaluated and which common conditions exist. In this answer, we will recap what @Conditional does, write a custom condition, and then go through the four common Spring Boot conditions and a few others with examples.

1A Quick Recap of @Conditional

@Conditional is a Spring Framework annotation that we place on a @Bean method, a @Component class or a @Configuration class. It names one or more Condition classes. When Spring processes the configuration, it calls each condition's matches method before registering the bean definition, and if any condition returns false, the bean or the whole configuration class is skipped as if it did not exist.

Notice the timing: conditions are evaluated while Spring reads configuration and registers bean definitions, before beans are created. So a condition can look at the Environment, the classpath and the bean definitions registered so far, but it must not depend on bean instances, which do not exist yet.

2Writing a Custom Condition

A condition is a class that implements Condition. Its matches method receives a ConditionContext, which gives access to the Environment, the bean factory, the class loader and resources, and the metadata of the annotated element.

Java
class OnLinuxCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String os = context.getEnvironment().getProperty("os.name", "");
return os.toLowerCase().contains("linux");
}
}
@Configuration
class FileWatchConfig {
@Bean
@Conditional(OnLinuxCondition.class)
FileWatcher inotifyWatcher() { return new InotifyWatcher(); } // registered only on Linux
}
// on Linux: context.getBeansOfType(FileWatcher.class).size() -> 1
// on macOS: context.getBeansOfType(FileWatcher.class).size() -> 0

This is all that Spring Boot's conditions are: Condition implementations wrapped in friendlier annotations. Spring Boot's own conditions also record why they matched or did not, which is what feeds the condition evaluation report we see with --debug.

3Classpath Conditions: @ConditionalOnClass and @ConditionalOnMissingClass

@ConditionalOnClass applies only when the named classes are on the classpath. It is the reason adding a dependency switches on a feature: the Jackson auto-configuration applies only if ObjectMapper is present. @ConditionalOnMissingClass is the opposite and applies only when a class is absent.

Java
@AutoConfiguration
@ConditionalOnClass(name = "io.lettuce.core.RedisClient") // only if Lettuce is on the classpath
class RedisCacheAutoConfiguration { ... }

Using the class name as a string, or putting the annotation on a configuration class rather than a method in a class that imports the type, matters: the condition must be evaluated before Java tries to load a class that might not exist. Spring Boot reads these annotations from bytecode metadata so that a missing class does not cause an error.

4Bean Conditions: @ConditionalOnBean and @ConditionalOnMissingBean

@ConditionalOnBean applies only if a bean of a given type or name is already defined; @ConditionalOnMissingBean applies only if none is. The second is the heart of Spring Boot's 'back off' behaviour: Boot defines a default bean, and if we define our own of the same type, Boot's version is skipped.

Java
@AutoConfiguration
class ClockAutoConfiguration {
@Bean
@ConditionalOnMissingBean
Clock clock() { return Clock.systemUTC(); } // used only if the application defines no Clock
}
@Configuration
class TestClockConfig {
@Bean Clock clock() { return Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC); }
}
// with TestClockConfig: the fixed clock is the only Clock bean

These conditions check bean definitions registered so far, so the order of processing matters. That is why the Spring Boot documentation recommends using them only in auto-configuration classes, which are processed after all of the application's own configuration.

5Property Conditions and Others

@ConditionalOnProperty applies when a property has a certain value. With havingValue it compares the value; without it, any value except false matches. matchIfMissing decides what happens when the property is not set at all.

Java
@Bean
@ConditionalOnProperty(prefix = "app.audit", name = "enabled", havingValue = "true", matchIfMissing = true)
AuditListener auditListener() { ... }
// app.audit.enabled not set -> registered (matchIfMissing = true)
// app.audit.enabled=true -> registered
// app.audit.enabled=false -> skipped
Common Spring Boot conditions
AnnotationMatches when
@ConditionalOnClassThe classes are on the classpath
@ConditionalOnMissingClassThe classes are not on the classpath
@ConditionalOnBeanA bean of the type or name is already defined
@ConditionalOnMissingBeanNo such bean is defined yet
@ConditionalOnPropertyA property has the expected value
@ConditionalOnWebApplicationIt is a servlet or reactive web application
@ConditionalOnResourceA resource such as a file on the classpath exists
@ConditionalOnExpressionA SpEL expression is true

@ConditionalOnWebApplication lets a configuration apply only in a servlet or reactive web application, and @ConditionalOnNotWebApplication the opposite. @ConditionalOnResource checks that a resource, such as classpath:db/migration, exists. There are also @ConditionalOnJava for the Java version, @ConditionalOnCloudPlatform for platforms like Kubernetes, and @ConditionalOnSingleCandidate for when exactly one bean of a type can be injected.

6Follow-up questions

Can I combine several conditions on one bean?

Yes. All conditions on an element must match, so stacking annotations means AND. For OR logic, Spring Boot provides AnyNestedCondition, and for more complex rules AllNestedConditions and NoneNestedConditions, which we extend with nested classes that carry the individual conditions.

What is the difference between @Profile and @Conditional?

@Profile is itself implemented as a condition, ProfileCondition, that checks the active profiles. @Conditional is the general mechanism; @Profile is one specific, very common rule built on it.

Why should conditions not depend on bean instances?

Because they run while bean definitions are registered, before beans are created. A condition that calls getBean would force early creation of that bean, possibly before its own dependencies and post processors are ready, which leads to subtle bugs. Bean conditions in Boot look at definitions and their types instead.

7Conclusion

In this answer, we saw that @Conditional registers a bean or configuration only when its Condition's matches method returns true, evaluated while bean definitions are registered. We wrote a custom condition, then looked at Spring Boot's common ones: @ConditionalOnClass and @ConditionalOnMissingClass for the classpath, @ConditionalOnBean and @ConditionalOnMissingBean for other bean definitions, @ConditionalOnProperty for property values, and others such as @ConditionalOnWebApplication and @ConditionalOnResource.

What to say in the interviewAbout 80 seconds aloud

Open with

Conditional is the Spring Framework annotation behind most of Spring Boot's auto-configuration. I put it on a Bean method, a component or a configuration class and name a Condition class; while Spring registers bean definitions it calls the condition's matches method, which gets the Environment, the bean factory, the class loader and the annotation metadata, and if it returns false the bean or the whole configuration is skipped.

Then, if they want more

Because it runs before beans are created, a condition should look at configuration and definitions, never at bean instances. Spring Boot wraps this in ready-made conditions. ConditionalOnClass and ConditionalOnMissingClass check the classpath, which is why adding a dependency switches a feature on.

ConditionalOnBean and ConditionalOnMissingBean check bean definitions registered so far; ConditionalOnMissingBean is the back-off rule, so if I define my own Clock or ObjectMapper, Boot's default steps aside. These two are only reliable in auto-configuration classes, which are processed after user configuration. ConditionalOnProperty checks a property value, with havingValue and matchIfMissing to control the details.

Others include ConditionalOnWebApplication, ConditionalOnResource, ConditionalOnExpression and ConditionalOnSingleCandidate. When I need to know why something matched, I run with debug to get the condition evaluation report.

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.
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 "What does the @Conditional annotation do, and name four common Spring Boot conditions.": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/7ddb88d630/