Sub topic 4
Reacting to the classpath
This chapter assumes you know Spring @Configuration and @Bean classes, have run a basic Spring Boot application, and have read the earlier chapters on how auto-configuration classes are discovered and how @Conditional works.
Why the classpath matters
Spring Boot decides much of what to configure by looking at which classes are present. If a JDBC driver and DataSource are on the classpath, Boot can set up a data source. If they are absent, it should do nothing. Class conditions express this rule.
Two annotations cover it:
@ConditionalOnClassmatches when all named classes are present on the classpath.@ConditionalOnMissingClassmatches when the named class is absent.
Neither one looks at beans or properties. They only ask whether a class can be loaded.
Using @ConditionalOnClass
Here is the pattern from the greeting example. The auto-configuration applies only when GreetingService is available:
package com.example.greeting;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@AutoConfiguration
@ConditionalOnClass(GreetingService.class)
@ConditionalOnProperty(prefix = "greeting", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(GreetingProperties.class)
public class GreetingAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public GreetingService greetingService(GreetingProperties properties) {
return new GreetingService(properties.getMessage());
}
}The annotation sits on the class, so one check guards every @Bean method inside it. If the class is missing, Spring skips the whole configuration.
Class level versus method level
You can put @ConditionalOnClass in two places, and the two behave differently.
| Placement | What happens when the class is missing | Safe to name the class by `.class`? |
|---|---|---|
| On the `@Configuration` class | The whole configuration is skipped | Yes |
| On a `@Bean` method | Only that method is skipped | Only inside a nested class |
The reason is how Spring reads annotations. Boot reads class-level annotation metadata through ASM, a bytecode reader, so it can evaluate the condition without loading the class. That is why GreetingService.class is safe as a class-level attribute even when GreetingService is not present.
A method-level condition is different. Spring has to introspect the configuration class's methods, and a method signature that mentions a missing type can fail with NoClassDefFoundError.
A nested configuration for optional libraries
Suppose your auto-configuration works without a metrics library but adds extra beans when one is present. Isolate the optional part:
package com.example.greeting;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.micrometer.core.instrument.MeterRegistry;
@AutoConfiguration
public class GreetingMetricsAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(MeterRegistry.class)
static class MetricsConfiguration {
@Bean
GreetingMetrics greetingMetrics(MeterRegistry registry) {
return new GreetingMetrics(registry);
}
}
}When Micrometer is absent, Spring never loads MetricsConfiguration, so the missing MeterRegistry type is never touched. In your build file, mark the library as an optional dependency so it does not reach your users unless they add it themselves.
@ConditionalOnMissingClass
@ConditionalOnMissingClass takes the class name as a string, because the class by definition may not exist:
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingClass("com.example.FancyGreeter")
class PlainGreeterConfiguration {
}Use it for fallback behavior, such as a simple implementation that applies when a richer library is absent.
How the checks relate
Exercise
Your auto-configuration defines a @Bean method that returns MeterRegistryCustomizer, a type from an optional library. Why is @ConditionalOnClass on that method risky, and what is the fix?
Show answer
Spring must inspect the configuration class's method signatures, so a return type or parameter from a library that is missing can cause a NoClassDefFoundError before the condition is evaluated. The fix is to move the bean into a static nested @Configuration class annotated with @ConditionalOnClass, or to reference the class by name with @ConditionalOnClass(name = "...").
Exercise
Which annotation would you use to apply a simple fallback configuration only when a richer library is not on the classpath, and how do you name the class?
Show answer
Use @ConditionalOnMissingClass, and name the class as a string, for example @ConditionalOnMissingClass("com.example.FancyGreeter").