Spring Boot 2 automatic configuration principle

Table of contents

1. What is the function of @AutoConfigurationPackage annotation?

2. What is @Import(AutoConfigurationImportSelector.class) doing?

3. How does Spring MVC implement automatic configuration in Spring Boot?


Anyone who has used Spring Boot and is handsome knows that as long as there is @SpringBootApplication annotation on the startup class of the application, it is a Spring Boot application, and the required beans will be automatically configured. How did you do it?

The only clue is the @SpringBootApplication annotation itself. Ctrl + click to open this annotation, you will see that it has 3 other annotations attached to it (except @Target and other meta annotations), one of which @EnableAutoConfiguration is related to the automatic configuration we want to study, it opens the automatic configuration of the entire project switch.

How does this annotation enable automatic configuration? Continue to Ctrl + click to open the @EnableAutoConfiguration annotation, and you will see two annotations attached to it.

It seems that the secret is in it, we need to study these two annotations carefully . Check out the first one!

1. What is the function of @AutoConfigurationPackage annotation?

Click on this annotation, and you will see that there is nothing else on it, but an internal class named AutoConfigurationPackages.Registrar is imported.

Click on the Registrar class and find that it implements the ImportBeanDefinitionRegistrar interface of the Spring framework, then the registerBeanDefinitions() it implements will be called by Spring at startup. The register() method is called in registerBeanDefinitions() to see what the method name is registering, I guess. >_>

Take a look at the tricky operations in the register() method. Can't understand this code? You must make a breakpoint here to debug.

Look at this code, create a Bean through the code, the type is BasePackage (basic package), set the basic package name of this project cn.itrip, and finally register the Bean in the Spring IoC container, the Bean The name is org.springframework.boot.autoconfigure.AutoConfigurationPackages. In fact, this is another way of registering beans to the IoC container in Spring, which has the same effect as adding @Service and @Component annotations to the class.

To sum up, the main purpose of the @AutoConfigurationPackage annotation is to add the cn.itrip package to the collection of packages that need to be automatically configured. No wonder the same-level package where the startup class of the Spring Boot project is located will automatically configure the Bean, which is the entry added here.

2. What is @Import(AutoConfigurationImportSelector.class) doing?

First of all, you need to understand that the ImportSelector interface provided by the Spring framework is used to selectively import classes annotated with @Configuration. The AutoConfigurationImportSelector class imported here implements the ImportSelector interface, so its process() will be called by the Spring framework at startup.

Look at the screenshot of debugging above, the lower left corner is the calling path of the thread stack, and the order of calling is process() -> getAutoConfigurationEntry() -> getCandidateConfigurations(). We don't care what happens in the middle, but when we get to the getCandidateConfigurations() method, we can see that there is an Assert assertion here, and the error message says that the automatic configuration class cannot be found in the META-INF/spring.factories file.

Tracing this method further, it calls SpringFactoriesLoader.loadFactoryNames() -> loadSpringFactories () and indeed loads it in META-INF/spring.factories. So what's the treasure inside this file? Open this file and take a look, it is in Spring Boot's automatic configuration jar package.

You can see that the spring.factories file lists the automatic configuration classes of various modules, third-party frameworks or middleware that Spring can integrate, and the class names are all in the form of Xxx AutoConfiguration. These  Xxx AutoConfigurations are actually in the same jar package, and are responsible for the automatic configuration of beans for imported modules and third-party frameworks.

How to do it, let's take the SpringMVC framework commonly used in our Web projects as an example.

3. How does Spring MVC implement automatic configuration in Spring Boot?

We know that to use the Spring MVC framework in Spring Boot, you only need to import a spring-boot-starter-web starter. Traditional Spring XML needs to configure so many beans, and we don't need to configure them anymore, so is it the reason for automatic configuration? According to the previous section, first look at the automatic configuration of the web in the spring.factories file, and which automatic configuration classes are listed.

According to experience, look at the name and guess first, Spring MVC's front-end controller DispatcherServlet should be created by the first DispatcherServletAutoConfiguration class in the red box above. To confirm, open the DispatcherServletAutoConfiguration class and have a look.

The first thing you see are a few annotations on the class.

  • The @AutoConfigurationOrder annotation specifies the execution order, which is executed first;
  • The @Configuration annotation indicates that this class can be used to define various factory methods with @Bean;
  • The @ConditionOnWebApplication annotation indicates that the Bean of this class will only be configured in the Web project environment;
  • @ConditionOnClass(DispatcherServlet.class) indicates that the Bean of this class will only be configured if you import the dependencies of the Spring MVC package (the DispatcherServlet class can be searched under the classpath);
  • The @AutoConfigureAfter annotation specifies that in order to configure the Bean of this class, the Bean of the ServletWebServerFactoryAutoConfiguration class must be created first.

As can be seen from the constants in the code above, the automatically configured front controller, the Bean name in the IoC container is also called dispatcherServlet.

Of course, there is another class that has to be said about the automatic configuration of Spring MVC, which is  the WebMvcAutoConfiguration class. Beans such as view parsers and message converters that traditional Spring MVC needs to configure in xml are all created in this automatic configuration class.

Writing code every day is so boring, let's play WeChat video account together!

 

Guess you like

Origin blog.csdn.net/liudun_cool/article/details/108193207
Recommended