SpringBoot - principle (automatic configuration + principle analysis - source code tracking)

source code tracking

Enter from the startup class of Springboot for analysis.

 Source Tracking Tips

When you come into contact with various frameworks in the future, if you need to view the source code, you need to find the key points and core processes, first have a macro understanding of the entire principle and process, and then go to understand the details.

Press and hold Ctrl + left button to enter the source code

There are many annotations above the entire annotation. The above four are meta-annotations used to modify annotations, so you don’t need to read them.

Let's start the first annotation

@SpringBootConfiguration annotation   

There are also three source annotations in this annotation, don't look at them.

In addition, its main function is to assemble the @Configuration annotation, which is to declare the configuration class.

There is also an @Indexed annotation to speed up application startup. 

With this annotation, the startup class will also become a configuration class, so in the startup class, you can also declare the Bean object as follows and hand it over to the IOC container for management. 

 @ComponentScan

As mentioned earlier, the startup class has the function of package scanning. The reason is that the startup class has been configured with a component scanning annotation.

@EnableAutoConfiguration

In the previous article, it was mentioned that annotations like @Enablexxxx are generally encapsulated with annotations starting with @Import, which are used to import specified Beans or configuration classes. From the second half of it, we can see that this is the core annotation of automatic configuration .

From the following, we can see that the bottom layer of this annotation encapsulates an Import annotation, and value is the full class name of an implementation class of the ImportSelector interface.

AutoConfigurationImportSelector

In this implementation class, you will see that it implements a multi-layered ImportSelector interface.

A selectImports method is implemented in the ImportSelector interface, which returns a Stinrg array, which contains the classes that need to be imported into the spring IOC container.

The following is the interface implementation of the selectImports method

 I really don't want to read about how it is implemented. Let's look at it before the interview. The content of the source code is torn by hand.

Inside selectImports will return a method under this class, which has an error message,

From the above information, it can be seen that the required things are a spring.factories and springframework.boot.autoconfigure

No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. 

 In the third-party dependencies simulated in the previous article, you can see the following autoconfigure dependencies. This is the dependency for automatic configuration, that is, the configuration file dependency required above.

Another example is when using mybatis, there will also be an autoconfigure dependency under the dependency of mybatis as shown below 

 There is an external library in the left column, and there is a jar package officially provided by springboot. In it, you can see what kind of information these dependencies contain.

Looking down, you can see the two things you need above.

META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. 

//The file for loading configuration classes provided by SpringBoot later

spring.factories      

//The file of loading configuration class provided by SprngBoot early

In spring.factories,  as shown below, there are full class names of some classes.

 There are also some full class names in another file, which will eventually be loaded and read into the IOC container when the project starts and handed over to the IOC container for management. These class suffixes are all AutoConfiguration, which means automatic configuration. The bottom layer of these classes will encapsulate a

@Configure annotation, the specific implementation of the function is involved in the bottom layer

 For example, the following Gson is the underlying implementation provided by Google to process data in Json format.

 

 Then we can directly use it for injection after importing this dependency, but not all Bean objects under the configuration class will be registered in the IOC container. There will also be a @ConditionalOnMissingBean annotation like the above picture to indicate conditional assembly. Only after certain conditions are met will it be registered in the IOC container.

Summarize

The next article will look at how the @ConditionalOnMissingBean annotation performs conditional assembly.

Portal link:

Guess you like

Origin blog.csdn.net/m0_62327332/article/details/130904732