M04 Q16 How does Spring Boot know what to configure?
Spring Boot knows what to configure by usage of Auto Configuration Classes defined in starter modules. Spring Boot searches for META-INF/spring.factories
on classpath, whenever entry org.springframework.boot.autoconfigure.EnableAutoConfiguration
is encountered in this file, Auto Configuration Class pointed by this property is loaded.
Auto Configuration class is a regular @Configuration
class annotated with @ConditionalOn...
annotation which specifies under which conditions @Configuration
class should be loaded.
When conditions from @ConditionalOn...
annotation are matched, @Configuration
class is loaded which provides beans that integrates your application with specified technology.
Auto Configuration is often used with starter modules. Starter module provides set of dependencies, and optionally may provide Auto Configuration classes.
Spring Boot supports following Conditional Annotations for AutoConfiguration classes:
ConditionalOnBean
β presence of Spring BeanConditionalOnMissingBean
β absence of Spring BeanConditionalOnClass
β presence of class on classpathConditionalOnMissingClass
β 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 versionConditionalOnJndi
β if JNDI location existsConditionalOnWebApplication
β if a web application that uses WebApplicationContext or StandardServletEnvironmentConditionalOnNotWebApplication
β application that is not a web applicationConditionalOnProperty
β presence of spring propertyConditionalOnResource
β presence of resourceConditionalOnSingleCandidate
β only one candidate for the bean found