@ConditionalOnClass in Spring Boot
@ConditionalOnClass activates a Spring configuration or bean definition only when specified classes are present on the application's classpath. It is commonly used by Spring Boot auto-configuration to make optional integrations activate only when their required libraries are available.
`@ConditionalOnClass` is a Spring Boot condition used to test whether one or more classes can be found on the classpath. If the required class is available, the condition matches; otherwise, the guarded configuration is skipped.
It is often placed on an auto-configuration class or configuration method. For example, an auto-configuration for a library can require `SomeClient.class`, allowing the configuration to remain inactive when that library is not included in the application.
The annotation can specify classes with its `value` attribute or class names with `name`. Using `name` can be useful when the referenced class might not itself be available while the configuration class is being loaded.
A matching `@ConditionalOnClass` condition does not by itself create a bean. It only controls whether the associated configuration or bean definition is considered; other conditions and bean requirements may still determine whether the bean is ultimately created.
Key points
- Checks whether specified classes are present on the classpath.
- A matching condition allows the guarded configuration to be processed.
- A missing required class causes the guarded configuration to be skipped.
- Commonly used to implement optional library integrations in auto-configuration.
- `value` accepts class references, while `name` accepts class names.
Interview questions
Does `@ConditionalOnClass` guarantee that a bean will be created?
No. It only determines whether the class-based condition matches. The configuration may still have other conditions, missing dependencies, or bean-definition constraints that prevent a bean from being created. Therefore, classpath presence is a prerequisite in this case, not a guarantee of successful bean creation.
What does `@ConditionalOnClass` do?
`@ConditionalOnClass` is a Spring Boot condition that checks whether specified classes are available on the classpath. When the required class is present, the condition matches and the guarded configuration can be processed. When it is absent, Spring Boot skips that configuration. It is especially useful for optional integrations and auto-configuration.
Why is `@ConditionalOnClass` useful in Spring Boot auto-configuration?
Auto-configuration often supports libraries that an application may or may not use. `@ConditionalOnClass` lets Spring Boot activate the related configuration only when the library's classes are available. This prevents optional integrations from being applied to applications that do not have the required dependency. It therefore helps auto-configuration remain modular and conditional.
What is the difference between `value` and `name` in `@ConditionalOnClass`?
`value` specifies the classes directly using Java class references. `name` specifies classes by their fully qualified class names as strings. A string-based check can be useful when referencing a class directly could create class-loading problems. The choice therefore depends partly on when and how the referenced class must be resolved.
How would you design an optional integration using `@ConditionalOnClass`?
I would put the integration configuration behind `@ConditionalOnClass` for the library types that identify the integration. Then I would add any additional conditions needed for properties, existing beans, or user overrides. The goal is for the auto-configuration to activate only when its prerequisites are satisfied. I would also keep the integration isolated so applications that do not include the optional dependency do not need to load its configuration.