Make writing a habit together! This is the 13th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.

directory

What does Spring do when Tomcat starts the project

preface

In the last article, we talked about what DispatchServlet does in init, so here’s a quick summary.

  1. Create WebApplicationContext
  2. Execute the Refresh method of applicationContext
  3. Methods that begin with init in DispatchServlet include, but are not limited to, the following methods.
  • initHandlerMappings
  • initHandlerAdapters
  • initViewResolvers
  • .

The official start of the

First, let’s recall the configuration of web.xml.If you look at the configuration information in the box, you should know what it means. Requests that start with URL/go to DispatchServlet.

So think about the Servlet lifecycle here. Init -> service -> doGet/doPost -> doGet/doPost -> doGet/doPost I’m going to make a simple Get request here.

http://localhost:8080/spring_mvc_war_exploded/request

I then looked for the doGet method according to the lifecycle criteria, but I did not find the corresponding method in DispatchServlet, so I looked at its parent class at this point.By looking at it, all the request methods point to a processRequest method.

processRequest

At the beginning of the method, some parameters are registered, basically all declared above, the following method is used, we do not go into the details of the method inside.

We’re not going to see a catch, but we’re going to see a try with a doService method, which looks like it’s a key method to execute code. The next thing worth noting is the last line of code in finally. Broadcast a RequestHandled event, at which point the request processing should be finished, so push an event and let’s go in and take a look.Push is a ServletRequestHandledEvent, see here like this should be used to do a simple request logging, such as the end of the article, write an event listener to look at.

DoService method

This is an abstract method and we can look directly at the implementation in DispatchServlet.

As soon as the method goes in, it prints a log that I don’t have to look at here.

Then an If judgment comes into view. It is also easy to judge whether IncludeRequest is included in the request, which we obviously did not set, so leave it alone. False is returned.

And even if you do, it looks like you’re just putting some data into Attributus Napshot, but I don’t know what that data is, so it doesn’t matter if you look at the next piece of code.

Here we add a few values to the request properties, applicationContext, localeResolver, themeResolver, and themeSource. The first is understandable, and the others are unknown and unused. Just remember that there are already several attributes in the request.

This code is the same as the previous code, but it’s also like putting properties in the Request object, so just keep those properties in mind.

This code does not understand what is going on. It assigns a value to a variable declared outside, so skip it and use it later.

This is the last piece of code, a doDispatch method ina try, just like the doService method.

DoDispatch method

The method starts by declaring some variables, which gives you a sense of what you’re doing.

Here we focus on the latter two methods. The checkMultipart method checks for Multipart (i.e. file upload Request) and then assigns the value returned to processRequest at the beginning of the creation process. That is, a new Request object might be returned inside this one, so just take a quick look at it. Sure enough, if the Request is multipart, a new Request is created and returned, which explains why the last line of code is ProcessedRequest! = the request.

Here the code gets the Handler from the Request and needs to go back and look at the code.

getHandler

/ / GetHandlerMappings (); / / GetHandlerMappings (); / / GetHandlerMappings ();It’s easy to see here that we need to call the method in the RequestMappingHandler, and then navigate to this class.

Look at the RequestMappingHandlerMapping a class diagram, simply write down the useful. This method is in the AbstractHandlerMapping class, so let’s go straight to it.

The Handler of the getHandlerInternal channel is the key to the problem.

This is an abstract class, click to see which classes implement methods, according to the class diagram to find.

Is determined based on the class diagram, AbstractHandlerMethodMapping method in a class, then the implementation.

getHandlerInternal

The first line of code simply gets the address that the request will match. The lookupHandlerMethod then finds the specific handling method based on the requested address, which is the specific Controller method.Then we get the Handler and we return it.

DoDispatch method.

Then use the mappedHandler you just got to get the HandlerAdapter and look at the code.

getHandlerAdapter

The idea here is to go through all the Adapters in the current class and see which one supports the current handler.In the current class has three Adapter, one soon in the past, it is clear that we are RequestMappingHandlerAdapter, then return back.

DoDispatch method.

I’m going to call the applyPreHandle method, and I’m going to follow it up here.One thing that looks familiar here is interceptor, a request interceptor that should be used by anyone who has ever used MVC.

The Handle method of the HandlerAdapter is then called.

Come to an end

This article is too long to be easy to read. Keep reading and keep watching for the next article. Thank you

See this, give it a thumbs up before you go, Bao ~

conclusion

The purpose of writing an article is to help you consolidate your knowledge. You can point out your bad or wrong writing in the comments section. If you read the article and think it is helpful to you, you can like it. If you find some questions and have doubts, or do not understand, you can comment and add my wechat. Of course, I also hope to make friends with you and learn from each other.