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.