This is the 17th day of my participation in Gwen Challenge
(^ U ^) Blue ~YO, today 4ye share this Springboot auto assembly with friends ~β
website
Let’s see how the official website defines this autowiring ~
Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added
You can see here that when you introduce a JAR dependency, Springboot tries to automatically configure the Spring application based on that dependency.
You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configuration classes.
It is also stated that you should select one of the @EnableAutoConfiguration and @SpringBootApplication annotations on a Configuration class @Configuration for the autoconfiguration to take effect.
So, after knowing these information, we began to look at the source link happily ~ blunt blunt! π
By the way, the Springboot version of Exploration is 2.4.5
@SpringBootApplication
If you go to the @SpringBootApplication annotation, you can see that it is a composite annotation. In addition to the first four basic meta-annotations, there are three @SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan
The source code is shown in figure
@SpringBootConfiguration
Let’s look at this first annotation, as shown here, and see that it’s actually an @Configuration annotation,
The @Configuration annotation is used as a Configuration class to configure the Spring context, equivalent to
in Spring’s XML Configuration file.
The source code is shown in figure
@ComponentScan
The purpose of this is to scan the components in the specified path and add them to the IOC container
The source code is shown in figure
You can see that there is a @REPEATable (ComponentScan) annotation inside it, which indicates that the @ComponentScan annotation is Repeatable
Note that some classes are excluded in our own custom way by implementing the TypeFilter interface and overwriting the match method.
Small details
Here is another point to note ~π
AutoConfigurationExcludeFilter will check the configuration, if the configuration and the meta-inf/spring. The factories in the file EnableAutoConfiguration class is the same, the corresponding configuration will be rule out ~
@EnableAutoConfiguration
Finally, come to the focus of this article π·
You can see from its name that it is the protagonist of this autoconfiguration
The source code is shown in figure
You can see that it is a composite annotation
Let’s start with the annotation 1 @AutoConfigurationPackage
@AutoConfigurationPackage
As the name suggests, it is an annotation that automatically configures the package path
The source code is shown in figure
As you can see from this comment, when the basePackages or basePackageClasses are not configured, the class will automatically register the package in which the annotation is located as the base path
Then, we take a look at the contents of this box ~ @ Import π· (AutoConfigurationPackages. The Registrar. The class)
@Import
Take a look at the function of this annotation π
The source code is shown in figure
Can be found that its role is also very simple, it is an import component, such as common @ the Configuration class or ImportSelector and ImportBeanDefinitionRegistrar implementation class, Or some other generic Component like @Component, @Service, etc
Registrar
Continue to see this @ Import (AutoConfigurationPackages. The Registrar. The class), can be found that the annotation of Import is the Registrar class π·, then we continue to explore, take a look at what did it do ~ π
The source code is shown in figure
As can be seen from the annotation of the class, its role is through ImportBeanDefinitionRegistrar * * to save the basic package path * *
So, once we’ve done the first one, we’ve just seen that this @AutoConfigurationPackage is used to configure the base package, and then we can look at the second one, The @ Import annotations (AutoConfigurationImportSelector. Class).
We can find that it imports the AutoConfigurationImportSelector class.
AutoConfigurationImportSelector
From the name, you can get a sense that it is a component selector
Key Steps
process
This method is called when fetching the Import classes, as shown in the flowchart at the end ~
getAutoConfigurationEntry
Gets the auto-configuration entity class, which also contains the focus of this article
getCandidateConfigurations
It calls the SpringFactoriesLoader, which is the SPI mechanism for Springboot. !). What is different about the Java Spi mentioned in
Read on ~π
Springboot SPI mechanisms
Old rules, see a note haha π first
Spring. Factories file
For example, π
# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener
Copy the code
You can use the meta-INF /spring.factories file with the following differences:
-
You can see a big difference in the name (one is a factories file, the other is named after the full name of the interface).
-
Spring. factories act as an aggregation of interfaces and implementation classes in the spring.factories file as key = value.
-
All the configuration items in the spring.factories are loaded into our cache as Map
,list
>, but not all are instantiated and loaded into the IOC container, In addition to the necessary class (EventPublishingRunListener, etc.), and to meet certain conditions of automatic configuration classes will be loaded into the IOC container
β Satisfy certain criteria such as whether the dependency (starter in the POM) is used or not
Auto-configuration classes refer to those classes ending in AutoConfiguration
This on-demand implementation mechanism is much more flexible than Java’s one-size-fits-all implementation ~ π
Instance analysis
Here, for example, 130 auto-configuration classes are loaded from the Spring.factories file
But in actual use, after filtering, there are only 30
There is also some sorting by priority ~ π·
Automatic assembly flow chart
There are only a few key steps here, the specific process is too long, the SpringApplication source code refreshContext(context); Here is not introduced first, later have time to write to share this [[Springboot source boot process]]
The class name and method block in the same color correspond to ~
The processImports method in the lower left corner of the figure instantiates these auto-configuration classes, including @import, @bean, etc., into the Spring IOC container step by step.
conclusion
It is important to enable the @enableAutoConfiguration or @SpringBootApplication annotation on the configuration class for the automatic configuration to take effect.
2. At the heart of the automatic configuration is Springboot SPI mechanisms, as well as the component selector AutoConfigurationImportSelector, Concrete is through the getAutoConfigurationEntry method to get the automatic configuration of SPI classes and filtering, at last, by processImports will configure class loading to the IOC container, complete the automatic configuration
The last
Finally no more text ha ha ~ π
If you think this article is good, please give it a thumbs-up π
Let’s start this unexpected meeting! ~
Welcome to leave a message! Thanks for your support! γΎ(β§β½β¦*)o go!!
I’m 4ye and I’ll see you soon next time!!