Spring Boot automatic configuration principle (Spring Boot interview questions)

Table of contents

1. to have doubts 

2. Source code analysis

2.1, pom.xml file analysis

2.2. Startup class analysis

3. Summary


 

1. to have doubts 

When we use Spring Boot for the first time to practice, we will feel that Spring Boot is so powerful. Whether it is dependency or configuration, it is ready for us. We only need to write business code, which saves the tedious configuration. So the reason why Spring Boot can provide us with these operations, how is it implemented?

2. Source code analysis

2.1, pom.xml file analysis

We open the pom.xml file after creating the Spring Boot project

You can see that there is a parent tag here, which is the parent project spring-boot-starter-parent, which is called the starting dependency of Spring Boot , so what is the starting dependency, let's use ctrl+left button to open it

You can see that it is inherited from a spring-boot-dependencies dependency, which is the core dependency of Spring Boot, let's continue to open

It can be seen that a large number of dependencies and plug-ins are integrated here, as well as the versions of each dependency, a very large file with more than 3,200 lines. This is the core dependency library of Spring Boot. The reason why we don’t need to manually import dependencies , that is, Spring Boot has integrated a lot of what we can use into this library. The reason why we don’t need to declare the dependent version is because Spring Boot has done a unified version control. Different versions of Spring Boot will have different versions. dependency. This is also one of the characteristics of Spring Boot that convention is greater than configuration.

Spring Boot also has many startup dependencies, spring-boot-starter is just one of them

<!--spring-boot启动器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<!--springboot-web启动包-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot-xxx启动包-->

2.2. Startup class analysis

With the support of dependencies, how does Spring Boot implement automatic configuration?

This requires us to study the @SpringBootAppliation annotation in the startup class

We ctrl + left mouse button to open the source code of the annotation

You can see that it is in the form of a custom annotation

@Target(ElementType.Type) returns an array of element types that can be applied to the annotation type

@Retention(RetentionPolicy.RUNTIME) indicates how long the annotation annotated by it is retained

annotation illustrate
SOURCE Annotations are only kept in source files. When Java files are compiled into class files, annotations are discarded; ignored by the compiler
CLASS Annotations are kept in class files, but are discarded when jvm loads class files. This is the default lifecycle
RUNTIME The annotation is not only saved in the class file, but still exists after jvm loads the class file

@Documented   

Indicates that annotations with types are documented by default by javadoc and similar tools. This type applies to declarations of annotation types that affect the usage of the annotated element by its clients.

@Inherited 

Indicates automatic inheritance of annotation types.

@SpringBootConfiguration (core annotation)

The annotation function: 

1. Indicate that this class is a configuration class
2. Scan the configuration class with @Configuration added.

@EnableAutoConfiguration (the core annotation)  to enable automatic configuration

We open the AutoConfigurationImportSelector class in the @Import annotation

Find the selectImports method, which calls a getAutoConfigurationEntry method

We ctrl + left button to find this method, find the getCandidateConfigurations method

Find the loadFactoryNames method in the method 

After searching layer by layer, I finally found the path to obtain resources, which is in the META-INF/spring.factories file

The essence of Spring Boot auto-assembly is the process of reading the configuration class file saved in META-INF/spring.factories through Spring and then loading the bean definition.

In the jar package, let's look for this file

As you can see, there are a lot of configurations in it, among which the common ElasticSearch, MongoData, Redis, and SpringData are in the red box. . .

That's why Spring Boot doesn't need to let us focus on configuration anymore

So so much configuration information, will it be configured for us when Spring Boot starts? Do you need to configure the unused configuration? In fact, Spring Boot has already considered this point. These configuration information will only be configured for us if we add relevant dependencies.

So what does Spring Boot use to load the corresponding configuration information according to the added dependencies?

We can find a third-party configuration class to take a look, here I choose redis

As you can see, there is an annotation here that I haven’t seen before, @ConditionalOnClass annotation, the function of this annotation is to check whether to import related dependencies, if not imported, each method of this class will report an error

3. Summary

After the above analysis, Spring Boot's automatic assembly principle is summarized in one sentence:

When Spring Boot starts, the main configuration class is loaded, and the @EnableAutoConfiguration annotation will scan "META-INF/spring.factories" under the class path of all jar packages through the @Import annotation. The automatic configuration class is marked in it, and the automatic configuration class will pass
@ The ConditionalOnClass annotation detects whether related dependencies are added. If related dependencies are added, the bean in the automatic configuration class will be added to the container to complete the automatic configuration.

Guess you like

Origin blog.csdn.net/select_myname/article/details/128191527