Explain the startup process of SpringMVC's "modern" way

There is no such thing as a quiet time, but someone is carrying the load for you", this sentence is not false. What should be done is still to be done, you don't have to do it, it is done for you by others.
When SpringMVC meets After SpringBoot, it is really convenient for developers, because SpringBoot writes all the codes and applies them through automatic configuration. Because the
modern startup process
is based on SpringBoot, it is necessary to follow SpringBoot's Style, Everything starts with the entry class SpringApplication.
First create the container, as shown in Figure 01 below:

The name of this container class is:

AnnotationConfigServletWebServerApplicationContext


It is specially "tailored" by SpringBoot. Compared with the previous container class, the biggest difference is that it has to create and start Tomcat.
Create and start Tomcat when the container is refreshed, as shown in Figure 02:

During the startup process, take the ServletContext object out of Tomcat, because it is created by Tomcat.
When creating Tomcat, the Spring container instance itself is actually passed in, and then brought out through a method parameter. As shown in Figure 03:

Then put the Spring container into the ServletContext, as shown in Figure 04:

At the same time, the ServletContext is saved to a field of the Spring container instance for subsequent use.
Then register the core servlet as a Bean, as shown in Figure 05:

Then register the core servlet in ServletContext, which is equivalent to registering in Tomcat, as shown in Figure 06:

Then look at the specific registration process, add the core Servlet to the ServletContext, as shown in Figure 07:

Configure URL mapping and file upload, etc., as shown in Figure 08:

The URL mapped by the core controller is still "/", as shown in Figure 09 below:


The modern way vs the traditional way is
the same:
both need to create a Spring container
, both need to put the container into the ServletContext, and
both need to register the core Servlet in the ServletContext.
Difference:
The traditional way can create two Spring containers, the modern way only has one container.
The traditional way is that Tomcat starts first, and then drives the creation of the Spring container. The modern way is that the container is created first, and then drives Tomcat to start when it is refreshed.
Important point:
Although the timing and method of starting Tomcat are different, the method of fetching ServletContext from Tomcat is the same, and the "little bridge" interface mentioned in the previous article must be used.
It is used to trigger some initialization work, mainly to register the core Servlet into the ServletContext, as shown in Figure 10 below:

What is left for developers?
In general uncomplicated cases, there is only one application.yml configuration file left. Developers can configure it in it.
These configuration items must be preset in advance, and these configuration values ​​will be read at startup and applied in initialization, as shown in Figure 11 below:

Of course, there are many configurable items, and the IDE has smart prompts, which is very convenient.

The author's voice: The framework is getting more and more perfect and intelligent, and the work left for ordinary developers is almost only CRUD, work hard, otherwise it will really only be this.

Original link:
https://mp.weixin.qq.com/s/QaV57dfnWmKuJmruUBDpjg
Author: Programming New Talk

Guess you like

Origin blog.csdn.net/m0_67645544/article/details/124409215