In a Web application based on the Spring framework, how is Spring's application context configuration file applicationgContext.xml
automatically loaded?
When running a Web project, the application server (JBoss, Tomcat, etc.) will first read the web.xml
file in the project source code path , parse the configuration in it, and find the configuration ContextLoaderListener
, so ContextLoaderListener
the contextInitialized
method 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.xml
to the contextConfigLocation
properties configured in the method applicationContext.xml
, and the Spring container will be initialized according to this file.
If there web.xml
are no configuration contextConfigLocation
parameters in it, can't the applicationgContext.xml
file be loaded ?
If there are no configuration contextConfigLocation
parameters, the application will search for the /WEB-INF/applicationContext.xml
file 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 initWebApplicationContext
the interior of the initialization method will context
save to ServletContext
in particular is kept to a Map
type attribute, key
is WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
, value
is a specific instance of an object WebApplicationContext.
If you want to ServletContext
get this in the code WebApplicationContext
, how to do it?
The Spring framework provides a WebApplicationContextUtils
tool class, which getWebApplicationContext
can be obtained through the method of this tool class .
Just now ServletContext
, what is it used for?
ServletContext
Some methods are defined to facilitate communication Servlet
with the Servlet
container. In a web application, all Servlet
are common ServletContext
. When Spring is used in conjunction with a web application, the Spring container is stored ServletContext
in it. In layman's terms, one is ApplicationContext
stored ServletContext
in In a Map property.
There had to understand web.xml
in Listener
, Filter
and Servlet
initialization 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 Servlet
instantiated in order from the smallest value to the largest value , and the corresponding init()
method is called .
Speaking of Servlet
, DispatcherServlet
did anyone in Spring MVC know? Tell me about its implementation principle?
DispatcherServlet
It 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 DispatcherServlet
initialization will execute the init
method. DispatcherServlet
The init
method found in the source code inherits from it HttpServletBean
. In this initialization method, an WebApplicationContext
object will be instantiated , and the initialized will be context
stored ServletContext
in it Servlet
to associate with the Spring container. In DispatcherServlet
the onRefresh
method, 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 WebApplicationContext
found from it and cannot be found In the case of reloading , the various strategies in the DispatcherServlet
same directory DispatcherServlet.properties
, such as initialization HandlerMapping
, registration of various request processing strategies and processing classes.
Specifically, how does DispatcherServlet
request distribution work?
First, the Spring MVC framework traverses all beans in the Spring container when it starts, traverses the methods in the annotated @Controller
or @RequestMapping
annotated class, @RequestMapping
merges the annotation values on the class and method , and uses @RequestMapping
the relevant parameter values of the annotation (such as value
, method
etc.) a package RequestMappingInfo
, this Controller
example, the method and the method parameter information (type, annotations, etc.) into the package HandlerMethod
, and then to RequestMappingInfo
as key
, HandlerMethod
to value
keep to a to Map
a structure of handlerMethods
the.
Next, the @RequestMapping
annotation of value
(i.e., a path request) values taken, that is url
, and then url
to key
order RequestMappingInfo
to value
, in a deposit to Map
a structure of the urlMap
property.
When the client sends a request, in accordance with a request URL
to urlMap
find, locate RequestMappingInfo
, and then according RequestMappingInfo
to the handlerMethods
lookup, to find the corresponding HandlerMethod
, then HandlerMethod
encapsulated HandlerExecutionChain
; then iterate through all containers HandlerAdapter
implementation class, find support this request HandlerAdapter
, such as RequestMappingHandlerAdapter
, then execute The front method ( preHandle
method) of the Spring MVC interceptor , then parses and converts the request parameters, and then (using reflection) calls the specific Controller
corresponding method to return an ModelAndView
object, executes the interceptor's post method ( postHandle
method), and then returns the result Perform processing, and finally execute the afterCompletion
method.