[SpringMVC source code analysis] DispatchServlet initialization principle process

Check the inheritance relationship of DispatchServlet as follows:

As mentioned above, all servlets are implementation classes of the Servlet interface, so they all have an init method.

When our servlet is registered into the servlet container, the init method will not be executed immediately

t omcat source code:

But tomcat will call the init method when the servlet is called for the first time to process the request

Called down in turn, initServletBean

Called down in turn, initWebApplicationContext initializes the Web application context, there is an onRefresh method

Execute the onRefresh method of the DispatchServlet class

Finally, various components are initialized, such as file upload parser, processor mapper, processor parser, view parser, etc.

These components are taken from the ApplicaitonContext, which is taken from the IOC container.

 

When were these components put into the IOC container?

It is the configuration class and @bean annotation in the WebMvcAutoConfiguration class of springboot, so when it starts, spring will create the bean in the ioc container.

 

If you want to extend, you only need to implement the corresponding interface and register it in the container. If you implement the MultipartResolver interface, you can customize the file upload parser.

Registering directly to the container can take effect because springboot implements its automatic configuration. It can be known from the @ConditionalOnMissingBean annotation that when an implementation class of the MultipartResolver interface already exists, it will not be initialized.

Guess you like

Origin blog.csdn.net/sumengnan/article/details/113773715