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.