Spring Boot
September 10, 2021
M04 Q23 What are the examples of @Conditional annotations? How are they used?
Spring Boot supports following Conditional Annotations for AutoConfiguration classes:
ConditionalOnBean
β presence of Spring BeanConditionalOnMissingBean
β absence of Spring BeanConditionalOnClass
β presence of class on classpath
ConditionalOnMissingClass
β absence of class on classpathConditionalOnCloudPlatform
β if specified cloud platform is active β for example Cloud FoundryConditionalOnExpression
β if SpEL expression is trueConditionalOnJava
β presence of Java in specified version
ConditionalOnJndi
β if JNDI location existsConditionalOnWebApplication
β if a web application that uses WebApplicationContext or StandardServletEnvironmentConditionalOnNotWebApplication
β application that is not a web applicationConditionalOnProperty
β presence of spring property
ConditionalOnResource
β presence of resourceConditionalOnSingleCandidate
β only one candidate for the bean found
@Conditional
annotations are used together with Auto Configuration Classes, to indicate under which conditions, specific @Configuration
class should apply.