Make writing a habit together! This is the 12th 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 how in ContextLoaderListener, his Initizlized created a webApplicationContext and put it into the servletContext. When Tomcat starts up and runs, the first method will enter the listener, and the Servlet will be created one by one. Remember the life cycle of servlets, the first method is init, and the next method is read one by one.

The first thing to do is figure out where the Init method is, so let’s look at DispatchServlet.There are many methods that start with init that do not exist, so look at their parent class FrameworkServlet.Not found in this class either, then turn to the parent HttpServletBean.Sure enough, we implement Init, we find a place and we make a breakpoint to make sure it’s this entry, and we start reading.OK, the breakpoint is in, make sure this is the entry point, and start the formal reading of the code.

The official start of the

The init method

This code is relatively simple; propertyValues is the data from the init-param tag retrieved from the servlet tag. We don’t have a setting here, so If we don’t get it here it won’t go into the If below.Next here is a template method, implemented by subclasses, which is implemented in the FrameworkServlet that we will read here.

initServletBean

By looking at the code, in addition to print log, only this initWebApplicationContext method is the key, we click in to look at.

initWebApplicationContext

Look at the first paragraph.This code is relatively simple. It creates two variables, one is rootContext and one is WAC, both of type WebApplicationContext. A WebApplicationContext is retrieved from the ServletContext, which is most likely the one created in ContextLoaderListener.

One of the properties of the current class is webApplicationConetxt, which is null because our current class was just created. The property value is not assigned. The If snippet will not enter.

Wac is empty at this time, will enter the If current, enter findWebApplicationContext method.

findWebApplicationContext

Looking at the code above, you can see that the key is whether the getContextAttribute method gets the value. As you can see, the contextAttribute default value is null, so it will still be null if no assignment is made. The method returns NULL, so the method returns NULL.

Go back to the previous layer of code.Create a rootContext that exists as the parent.

createWebApplicationContext

The first line retrieves an ApplicationContext class, most likely from web.xml, and returns a default if it doesn’t exist. An applicationCotext is then instantiated in the code below. The context is then set with the environment information, parent, and configLocation. Then call the configureAndRefreshApplicationContext method. The name of this method is to execute the refresh method. Finally back out, and then we see the follow-up configureAndRefreshWebApplicationContext method.

configureAndRefreshWebApplicationContext

This code is meaningless, setting an Id value.

This piece of code sets some property values and is not really useful for reference. Note that in the last line of this code, an event listener is added. The name of the event listener is ContextRefreshEvent.

Call an initPropertySources property, look at the name, and what this method does is treat the servletConfig as the propertySource, and then get the property from the servletConfig later.

Nothing here, the main is the refresh method, namely SpringBean that a set of things, is executed after the completion of all the way back to the subsequent initWebApplicationContext code. Let’s move on.

initwebApplicationContext

Now, if I go to this section, and I debug the code, and I find that this value is True, I’m going to skip the reverse.

The member variable publishContext, when viewed, defaults to true. What we are doing here is simply putting the newly created ApplicationContext into the ServletContext to prevent repeated creation.

It then returns to the final method.

doubt

At this time, I have a question, because I have also spent some articles about source code interpretation, there are such things as HandlerMapping, HandlerAdapter, ViewResolver, and then I wonder why I did not see it here, in self-doubt, is there something I missed? I looked around with the question.

Some method that starts with init

Some clues have been found in DispatchServlet. There are methods that start with init in DispatchServlet.

OnRefresh method

It turns out that these are some of the things I want, but they’re not being called. At this time I will look at their call relationship, resulting in the following figure.When you discover the OnRefesh method, you suddenly know where you’ve come across it.But in this case, I’m debugged, and RefreshEventReceived is equal to True. So I realize that I’ve changed the value of this property somewhere, and so I see something familiar.

Event listeners

Looking at this method, when RefreshEventReceived is set to True, the onRefresh method is also called. You can see it even here. It’s all strung together. Remember that in the previous method to create the applicationContext, you append a ContextRefreshListener to the applicationContext.If you look at the listener, it listens for the ContextRefreshEvent, which initiates an event broadcast at the end of Spring’s refresh method, called the ContextRefreshEvent.OK, so this is clear. I’ve strung together everything I’ve learned before.

Come to an end

So that’s the end of this article. The init method in DispatchServlet, you know what it is, if you’re interested in the details, you can read on. I have a cursory look at the beans, which are fetched via ApplicationContext and then put into a member variable of DispatchServlet.

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.