In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Pogy original, without permission, please do not reprint!!!
SpringMVC is a web-layer framework built on top of the Spring framework, responsible for sending each request to the appropriate handler, using the concept of views to ultimately return a response.
SpringMVC is the most important class DispatchServlet this class, when we open this source code, feel a face blinded. But sit back and take a look and see that this thing actually inherits some classes.
follow them
I don't think there's any leads. Keep talking.
At this point there is a major discovery, found that the DispatchServlet is ultimately inherited from HttpServlet, then we can analyze it according to the life cycle of ordinary servlets.
Servlet life cycle we all remember it, do not remember it does not matter, I will briefly list
1. First tomcat starts by querying the configuration file in web.xml to find the servlet configuration, and then creates an instance of the servlet with the new keyword.
2. Then call the servlet's init method to initialize, and then put the servlet into a container to manage, waiting to be used.
3. When the container closes, tomcat calls the servlet's destroy method to do some cleanup.
Ok, let's find the init method and see what's done in it.
There's nothing too complicated about the logic here, just loading the Configuration DispatchServlet configuration parameters into memory for spring to use. Some friends asked, what parameters
The main thing is that this calls the initServletBean method, which is empty in this class, and its subclass implements it, springmvc uses a lot of similar design patterns, called template method patterns. Simply put, it is written in the parent class, which methods you want to call, I will implement them in the subclass, so that the corresponding methods are called in the logical order of the parent class, and the subclass can have different implementations.
OK, let's find out which subclass implements this initServletBean method. Follow me...
Take a look at this HttpServletBean inheritance system as follows
Find FrameworkServlet first
We search for, really did not guess wrong, the realization of the method, written in the subclass, but also a lot, I will analyze it
Skip the logs and go down.
Call a method written in the current class initWebApplicationContext method, this method is very important, sit down, have a cup of tea, slowly enjoy spring this delicious meal.
Stick the source code up first,
The first step is to obtain the parent WebApplicationContext container through the tool class. Some friends asked where the parent container came from and when it was created.
It is created when this listener is running. We know that when the web server is started, the order of creation of the three major components is Listener>Filter >Servlet.
So here's where you can get to the parent container. Of course, you can also get this situation without discussing it, because it is impossible to say that you use springMVC instead of spring framework.
Here are a few if judgments, which I've labeled, to give you an analysis of the execution flow.
First of all, webApplicationContext must be empty, why, because you just started, it must be empty. So the first if will not be executed for the time being. Enter the second if, where findWebApplicationContext is called, in fact, a null will also be returned here, from the findWebApplicationContext name, it only looks up and does not create. Attach the source code, everyone has a look
So wac is still empty, so we go to the third if statement and we go to the createWabApplicationContext method to find out.
Here, code block 1 is log, code block 2 is Chinese judgment, code block 3 is to create a configurable web application context, that is, web container, code block 4 is to initialize this container. For example, the rootContext before setting is the parent container.
Finally, we return this created container, and our springMVC container is born. But it's not over yet. There's still if in the initWebApplicationContext() method of the Framework. I'll stick it up.
Next to the previous if serial number
Look at the initial value of this field.
So this onRefresh() method is to be executed, follow in and find empty implementations, unexpected things, it doesn't matter, template method patterns, find subclasses, there are clues,
This method again calls an initial Strategies (..) Method, what did this method do? Follow me in and see.
I'm just going to add a comment here.
Here, I'll just go in and have a look with one, initMultipartResolver (context), go in and have a look.
Pay attention to this code segment I drew. Let's consider whether there are any problems. Think carefully.
Of course there is a problem. We didn't inject this parser into the container. How can it get it? There's a ghost.
hehe
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.