Spring boot startup process analysis

introduction

As early as 15 years ago, we began to use spring boot for development. However, we have only used it for a long time, and we are ashamed not to have an in-depth understanding of the principle and how spring boot works. Today, let's start with spring boot and learn more about the working principle of spring boot.

Why spring boot

Before using a thing or a tool, we always ask ourselves, why should I use it? What good can he do me?

*The biggest advantage is that the spring boot follows the Java * * Convention and is larger than the configuration * * without facing a lot of configuration files. The spring boot determines what configuration to provide according to the package you use.

*The server is embedded in the project in the form of jar package. When microservices are flying all over the world, spring boot is naturally suitable for microservice architecture and easy to deploy.

*Providing devtools to change the code from now on requires restarting, which becomes history.

If there are advantages, there must be disadvantages. Disadvantages come from advantages and advantages come from disadvantages (I feel like talking about philosophical problems, ha ha ha)

*Because the configuration is not transparent to developers, it is not clear how spring boot performs such as JDBC loading and transaction management without looking at the source code. It is also difficult to adjust errors in case of errors.

*To customize the configuration after automatic configuration, you need to encode javaconfig and understand these configuration class APIs.

*The version iteration is too fast, and the new version makes too many changes to the old version, resulting in incompatibility, such as 1.3 Spring boot test before 5 and 1.4 Springboottest after 0.

Only an appropriate architecture is the best architecture. If you can accept the shortcomings of spring boot, spring boot is indeed a good choice to improve development efficiency.

Start process

After talking so much, it's time to get to the point. Let's see how spring boot starts and what it does.

The following code is the way to start the spring boot project standard. You can use the annotation @SpringBootApplication and invoke the run method of SpringApplication in the main method. Let's start with the run method to see the startup process of spring boot.

When we enter the run method, we can see that the new spring application (sources) is finally called run(args); new SpringApplication(sources). run(args); From this method, you can see that the startup of springboot can be divided into two parts. The first part is the instantiation of springapplication; Part 2: call the instance to run the run method. Let's take a look at the instantiation process of spring application.

The key points are two set methods**

setInitializers((Collection) getSpringFactoriesInstances(

ApplicationContextInitializer. Class)) * * and * * setlisteners ((Collection)

Getspringfactoryesinstances (applicationlistener. Class)) * * the two methods are the same. Pick the instantiation applicationcontextinitializer to talk about it.

The key to automatic configuration is the getspringfactoryesinstances method, specifically the loadfactorynames method in this method. Let's see what the loadfactorynames method does and how to realize automatic configuration.

You can see that this method does one thing from meta-inf / spring Take out all "URLs" from the path of factories. We can go to this path to see what it is?

Now everyone should understand that spring is dynamically configured by finding the applicationcontextinitializer it needs in all the jar packages you load. As long as you use a specific Maven package, you will find the meta-inf / spring under this package during initialization Factories need classes such as applicationcontextinitializer to instantiate beans. You can use them without any configuration.

Speaking of this, I have finished instantiating all spring applications, but there is another step after loading applicationcontextinitializer and applicationlistener, that is, to find the location of the startup class and set it into the property mainapplicationclass.

Next, let's go back to the new spring application (sources) Run (args) method to see how the run method runs.

The three key things to do from this method are:

Get listener and start

Load environment variables, which include system environment, classpath environment and application added by the user properties

Create ApplicationContext

There is nothing to say about the first two points. The third point is to create ApplicationContext. Creating ApplicationContext is divided into several parts: instantiating ApplicationContext, preparecontext and refreshcontext. Instantiating ApplicationContext will judge whether to use the webcontext class annotationconfigembeddedwebapplicationcontext or the ordinary context class annotationconfigapplicationcontext according to the webenvironment attribute we mentioned earlier (we use webcontext as an example here) then instantiate it through reflection. After instantiation of ApplicationContext, it will enter the preparecontext process. This preparecontext method will load the environment prepared before and enter the context. Then, if there are beannamegenerator and resourceloader, create beans in advance and load them into ApplicationContext, but this is generally the case Both are empty, so go directly to the applyinitializers method to initialize all initializers instantiated before. All beans are scanned and loaded here. This time, we will talk about the startup process, so we won't talk about it in detail. Finally, set the created ApplicationContext to the listener, and the preparecontext process is over. The last is refreshcontext, which is consistent with the spring bean loading process, including bean injection, beanfactory, postprocessbeanfactory, etc. for details, see the spring bean life cycle.

summary

There are still a lot of spring boot initialization contents, but to sum up, there are four points:

*Create a springapplication instance to determine whether the environment is a web environment or an ordinary environment. Load all initializers and listeners that need to be used. Here, the concept of convention greater than configuration is used to lift the veil of automatic configuration.

*Load environment variables, including system environment, classpath environment and application environment (that is, our customized application.properties configuration file)

*Create springapplicationrunlisteners

*Create ApplicationContext, set the assembly context, scan all beans here, and finally load and inject them during refreshcontext. Finally, set the assembled context as a property into spring applicationrunlisteners, which completes the startup of a spring boot project.

The above is the spring boot startup process introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>