The startup principle of SpringBoot

The startup principle of SpringBoot

A main program class and main entrance

(1) Main startup class code
Insert picture description here
(2) Description
@SpringBootApplication: Spring Boot application annotation on a certain class indicates that this class is the main configuration class of SpringBoot, and SpringBoot should run the main method of this class to start the SpringBoot application;
Insert picture description here

Detailed interpretation of the two main start-up annotations

@SpringBootConfiguration: Spring Boot configuration class;
marked on a class, it means that this is a Spring Boot configuration class;

@Configuration: Annotate this annotation on the configuration class; configuration class-----configuration file; configuration class is also a component in the container; @Component

@EnableAutoConfiguration: Turn on the automatic configuration function;
Spring Boot helps us automatically configure things that we need to configure before;

@EnableAutoConfiguration tells SpringBoot to turn on the automatic configuration function; so that the automatic configuration can take effect;
Insert picture description here
@AutoConfigurationPackage: automatic configuration package

@Import(AutoConfigurationPackages.Registrar.class):

Spring’s underlying annotation @Import imports a component into the container; the imported component is made by AutoConfigurationPackages.Registrar.class;

Scan all components in the package of the main configuration class (class marked by @SpringBootApplication) and all sub-packages below to the Spring container;

@Import(EnableAutoConfigurationImportSelector.class);

Import components into the container?

EnableAutoConfigurationImportSelector: the selector of which components to import; return all the components that need to be imported in the form of full class names; these components will be added to the container; a lot of automatic configuration classes (xxxAutoConfiguration) will be imported into the container; Import all the components needed for this scenario into the container, and configure these components;
Insert picture description here
with the automatic configuration class, we can avoid the work of manually writing configuration injection functional components, etc.;

SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);

When Spring Boot starts, it obtains the values ​​specified by EnableAutoConfiguration from META-INF/spring.factories under the classpath, and imports these values ​​into the container as the auto-configuration class. The auto-configuration class will take effect and help us with the auto-configuration work; Before we need to configure things by ourselves, the automatic configuration class will help us;

Three summary

SpringBoot is a framework that serves the framework. The scope of service is to simplify the configuration file
(1) @SpringBootApplication is composed of three annotations @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan.
(2) @SpringBootConfiguration: The internal source code provides @Configuration, which is the configuration class of the IOC container.
(3) @EnableAutoConfiguration: internally uses SpringFactoriesLoader to search for all META-INF/spring.fatories files from the classpath, and add Key[org. springframework.boot.autoconfigure.EnableAutoConfiguration] The corresponding Value configuration item is instantiated by reflection into the corresponding IoC container configuration class in the form of JavaConfig marked with @Configuration, and then summarized into the currently used IoC container.
(4) @CompenentScan: It is not necessary, its function is to automatically scan and load components or bean definitions, and load them into the container.

Guess you like

Origin blog.csdn.net/hcyxsh/article/details/114605196