This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact.
Based on Spring Boot 2.x, the principle of Spring Boot automatic configuration and @Conditional annotations are introduced in detail.
1 @SpringBootApplication Automatic configuration principle
@SpringBootApplication is a composite annotation, mainly composed of @ComponentScan, @SpringBootConfiguration, and @EnableAutoConfiguration. @enableAutoConfiguration is a key annotation for Spring Boot to implement automatic configuration.
@ComponentScan is used to scan specified package and subpackage paths and register qualifying component classes with the Spring container. The default package path is the current @ComponentScan package and its subpackages. The default filter is that a class is registered as a Component class if it has annotations such as @Component, @Service, @Repository, @Controller, @Configuration, etc. You can also customize conditions by specifying the includeFilters and excludeFilters attributes.
The @SpringBootConfiguration annotation is used to declare that the current class is a SpringBootConfiguration class and has the same effect as the @Configuration annotation. N @Configuration annotation Configuration classes can be added to an application, but only one @SpringBootConfiguration annotation Configuration class can exist. It is recommended to use @SpringBootConfiguration to represent the Boot class as a Boot application.
@enableAutoConfiguration is used to declare that the default Spring Boot configuration will be automatically loaded when the program starts.
@ EnableAutoConfiguration internal mainly with @ Import annotations introduced AutoConfigurationImportSelector class, This class uses the SpringFactoriesLoader tool to get configuration information from the/meta-INF /spring.factories file under spring-boot-autoconfigure.jar. Called the “org. Springframework. Boot. Autoconfigure. EnableAutoConfiguration” key value is a series of automatic configuration of the corresponding class of the full path name. In fact, all meta-INF/Spring.Factories under the Spring Boot Starter are read.
Spring will split the value into a collection of full pathnames, but instead of loading all the configuration classes, it will load the auto-configuration classes that conform to the rules into the Spring container.
When filtering, Conditional series annotations on the class are checked, @ ConditionalOnClass and @ ConditionalOnMissingClass, @ ConditionalOnWebApplication and @ ConditionalOnNotWebApplication, @ Conditional OnBean and @ ConditionalOnMissingBean and @ ConditionalOnSingleCandidate annotations (if present), only conform to the rules of automatic configuration class registered into the container.
We can also exclude the registration of certain auto-configuration classes through the exclude and excludeName attributes of @SpringBootApplication so that SpringBoot does not automatically register certain configurations. These auto-configuration classes are basically @Configuration annotations with a set of @Bean methods inside them. When loaded into the container, these auto-configuration classes are parsed and it is possible to perform a series of automated configurations.
2 @Conditional series of Conditional notes
The @Conditional annotation is used to determine whether a configuration needs to be skipped and can be used as a meta-annotation on other annotations, thus providing a rich validation mechanism for determining whether an auto-configuration class needs some auto-configuration.
The spring-boot-Autoconfigure package provides a series of annotations derived from @Conditional annotations, which have been annotated on various auto-configuration classes and are automatically configured when conditions are met. We can also use it directly; the common @Conditional series of annotations are as follows:
@ConditionalOnBean | This takes effect when a Bean of the specified type exists in the container |
---|---|
@ConditionalOnClass | The classpath takes effect if the specified class exists |
@ConditionalOnExpression | The SpEL expression takes effect when the result is true |
@ConditionalOnJava | Takes effect when the specified Java version exists |
@ConditionalOnJndi | Takes effect when the specified JNDI exists |
@ConditionalOnMissingBean | This takes effect when no Bean of the specified type exists in the container |
@ConditionalOnMissingClass | The specified class does not exist on the classpath |
@ConditionalOnNotWebApplication | Takes effect if the current project is not a Web project |
@ConditionalOnWebApplicatio | Takes effect when the current project is a Web project |
@ConditionalOnProperty | The specified attribute value takes effect when it matches the expected value |
@ConditionalOnResource | The classpath takes effect when the specified resource exists |
@ConditionalOnSingleCandidate | Only one Bean of the specified type is available in the container, or only one preferred Bean (@primary) is specified |
Related articles:
- Spring. IO /
- Spring Framework 5.x learning
- Spring MVC 5.x learning
- Spring Framework 5.x source code
If you need to communicate, or the article is wrong, please leave a message directly. In addition, I hope to like, collect, pay attention to, I will continue to update a variety of Java learning blog!