Spring MVC common interview questions

In a Web application based on the Spring framework, how is Spring's application context configuration file applicationgContext.xmlautomatically loaded?

When running a Web project, the application server (JBoss, Tomcat, etc.) will first read the web.xmlfile in the project source code path , parse the configuration in it, and find the configuration ContextLoaderListener, so ContextLoaderListenerthe contextInitializedmethod in the class will be executed , and the method will be called in this initWebApplicationContext()method According to the method name, we can see that this method is used to initialize a WebApplicationContext. The simple understanding is to initialize a Spring container under a Web application. In the initWebApplicationContext()implementation of the subsequent code of the method, the specified file will be loaded according web.xmlto the contextConfigLocationproperties configured in the method applicationContext.xml, and the Spring container will be initialized according to this file.

If there web.xmlare no configuration contextConfigLocationparameters in it, can't the applicationgContext.xmlfile be loaded ?

If there are no configuration contextConfigLocationparameters, the application will search for the /WEB-INF/applicationContext.xmlfile in the root directory of the application by default , that is to say, this is a file path that is loaded by default.

After the initialization of WebApplicationContext under this Web application is completed, what does it have to do with ServletContext?

In initWebApplicationContextthe interior of the initialization method will contextsave to ServletContextin particular is kept to a Maptype attribute, keyis WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, valueis a specific instance of an object WebApplicationContext.

If you want to ServletContextget this in the code WebApplicationContext, how to do it?

The Spring framework provides a WebApplicationContextUtilstool class, which getWebApplicationContextcan be obtained through the method of this tool class .

Just now ServletContext, what is it used for?

ServletContextSome methods are defined to facilitate communication Servletwith the Servletcontainer. In a web application, all Servletare common ServletContext. When Spring is used in conjunction with a web application, the Spring container is stored ServletContextin it. In layman's terms, one is ApplicationContextstored ServletContextin In a Map property.

There had to understand web.xmlin Listener, Filterand Servletinitialization order it?

First, <listener>instantiate the listener class declared with the label, call the method of the listener class instance object contextInitialized(), initialize the application context data; then <filter>instantiate the filter class declared with the label, call the method of the filter class instance object init(); If a <servlet>tag is used in the <load-on-startup>tag, it is Servletinstantiated in order from the smallest value to the largest value , and the corresponding init()method is called .

Speaking of Servlet, DispatcherServletdid anyone in Spring MVC know? Tell me about its implementation principle?

DispatcherServletIt is the core distributor of SpringMVC. It implements request distribution and is the entry point for processing requests. It is one Servlet. When the application starts, the DispatcherServletinitialization will execute the initmethod. DispatcherServletThe initmethod found in the source code inherits from it HttpServletBean. In this initialization method, an WebApplicationContextobject will be instantiated , and the initialized will be contextstored ServletContextin it Servletto associate with the Spring container. In DispatcherServletthe onRefreshmethod, various request processing strategies are initialized, such as file upload processing strategy, URL request processing strategy, view mapping processing strategy, exception handling strategy, etc. Most of the execution logic of these strategies is first WebApplicationContextfound from it and cannot be found In the case of reloading , the various strategies in the DispatcherServletsame directory DispatcherServlet.properties, such as initialization HandlerMapping, registration of various request processing strategies and processing classes.

Specifically, how does DispatcherServletrequest distribution work?

First, the Spring MVC framework traverses all beans in the Spring container when it starts, traverses the methods in the annotated @Controlleror @RequestMappingannotated class, @RequestMappingmerges the annotation values on the class and method , and uses @RequestMappingthe relevant parameter values ​​of the annotation (such as value, methodetc.) a package RequestMappingInfo, this Controllerexample, the method and the method parameter information (type, annotations, etc.) into the package HandlerMethod, and then to RequestMappingInfoas key, HandlerMethodto valuekeep to a to Mapa structure of handlerMethodsthe.

Next, the @RequestMappingannotation of value(i.e., a path request) values taken, that is url, and then urlto keyorder RequestMappingInfoto value, in a deposit to Mapa structure of the urlMapproperty.

When the client sends a request, in accordance with a request URLto urlMapfind, locate RequestMappingInfo, and then according RequestMappingInfoto the handlerMethodslookup, to find the corresponding HandlerMethod, then HandlerMethodencapsulated HandlerExecutionChain; then iterate through all containers HandlerAdapterimplementation class, find support this request HandlerAdapter, such as RequestMappingHandlerAdapter, then execute The front method ( preHandlemethod) of the Spring MVC interceptor , then parses and converts the request parameters, and then (using reflection) calls the specific Controllercorresponding method to return an ModelAndViewobject, executes the interceptor's post method ( postHandlemethod), and then returns the result Perform processing, and finally execute the afterCompletionmethod.

Published 94 original articles · liked 0 · visits 722

Guess you like

Origin blog.csdn.net/qq_46578181/article/details/105458281