ServletContext
As the Web container starts, a corresponding ServletContext object is created for each Web application that represents the current Web application and provides the hosting environment for the spring IOC container.
The Web container reads Web. XML, creates ServletContext, and shares the context for all parts of the Web project as it is deployed. Context-param provides ServletContext with key-value pairs, which are information about the servlet context, which listener, filter, and servlet are likely to use, so load the Context-param first, Create the ServletContext, load the listener, load the filter, and finally load the servlet.
Next I will analyze the spring container startup process in this order of loading.
Contextloaderlistener
Web. XML is configured with Contextloaderlistener, or you can customize a listener class that implements the Servletcontextlistener interface, as shown in the configuration example in Web. Xml.
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
The Web container triggers the Servletcontextevent event during startup, is monitored by contextloaderlistener, and calls the Contextinitialized method in Contextloaderlistener , the Contextinitialized method is shown below.
public void contextInitialized(ServletContextEvent event) { this.initWebApplicationContext(event.getServletContext());}
The Contextloaderlistener class inherits the Contextloader, and in the process of initializing the context, Call Contextloader's Initwebapplicationcontext method to initialize the Webapplicationcontext. Webapplicationcontext is an interface, and spring's default implementation class is Xmlwebapplicationcontext,xmlwebapplicationcontext, which is the IOC container for spring.
Before initializing Xmlwebapplicationcontext, the Web container already loaded the Context-param instance in Context-param,web.xml as shown below. As spring's IOC container, its corresponding bean-defined configuration is exactly the Context-param specified.
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value></context-param>
The
then enters into the Initwebapplicationcontext method, and the Initwebapplicationcontext method is defined as follows (some code has been omitted).
public webapplicationcontext Initwebapplicationcontext (ServletContext ServletContext) {i F (Servletcontext.getattribute (webapplicationcontext.root_web_application_context_attribute)! = null) {throw new illegalstateexception (else{if (this.context = = null) {this.context = This.createwebapplicationcontext (ServletContext); }} servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute, this.context),
Before the spring IOC container is initialized, Initwebapplicationcontext detects whether the value with Root_web_application_context_attribute as key is empty, and if not NULL, initializes the IOC Context, and after the initialization is complete, the IOC CONTEXT is stored in the ServletContext with Root_web_application_context_attribute as key.
Initialize Servlet
Servlets can be configured in Web. XML, in spring, the most basic servlet is Dispatcherservlet, and the corresponding configuration instance is shown below.
<Servlet><Servlet-name>appservlet</Servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet </servlet-class> < init-param> <param-name> Contextconfiglocation</param-name> <param-value>/web-inf/spring/appservlet/appservlet-context.xml </param-value> </ init-param> <load-on-startup>1< Span class= "Hljs-tag" ></load-on-startup></servlet>
The
Dispatcherservlet will establish its own IOC context to hold the relevant bean, and in the process of initializing its own IOC context, first through webapplicationcontext.root_web_ Application_context_attribute, get Webapplicationcontext from ServletContext, The Webapplicationcontext as the parent context of the IOC context of the Dispatcherservlet. The initialization of Dispatcherservlet's own IOC context is done in the Dispatcherservlet Initstrategies method, including controller mapping, view parsing, and so on, as shown in the Initstrategies method.
protected void initstrategies (ApplicationContext Context) {this.initmultipartresolver (context); this.initlocaleresolver (context); this.initthemeresolver (context); this.inithandlermappings (context); this.inithandleradapters (context); this.inithandlerexceptionresolvers (context); this.initrequesttoviewnametranslator (context); this.initviewresolvers (context); this.initflashmapmanager (context);}
The type of Dispatcherservlet own IOC context is also xmlwebapplicationcontext, after initialization is complete, Spring will save the IOC context to servletcontext with the symbol associated with the Servlet-name property of the Dispatcherservlet as the key. This allows each servlet to hold its own context, which has its own bean space, while the servlet shares a key webapplicationcontext.root_web_application_ Context_attribute Webapplicationcontext, where the bean defined is a bean shared by each servlet.
//----------------------
First, for a Web application that is deployed in a Web container, the Web container provides its global context, which is ServletContext, which provides the hosting environment for the following spring IOC container;
Second, a contextloaderlistener is provided in Web. Xml. When the Web container starts, the container initialization event is triggered, and Contextloaderlistener hears the event and its Contextinitialized method is called, in which spring Initializes a startup context, This context is referred to as the root context, Webapplicationcontext, which is an interface class, specifically, its actual implementation class is Xmlwebapplicationcontext. This is the spring IOC container, whose corresponding bean-defined configuration is specified by the Context-param tag in Web. Xml. After the IOC container is initialized, Spring takes the Webapplicationcontext.rootWEBapplicationCONTEXTattribute as the property key, Store it in ServletContext for easy access;
Again, after the Contextloaderlistener listener is initialized, Start initializing the servlet configured in Web. XML, this servlet can be configured with multiple, for example, the most common Dispatcherservlet, the servlet is actually a standard front-end controller to forward, match, and process each servlet request. The Dispatcherservlet context, when initialized, establishes its own IOC context for holding spring MVC-related beans. The Webapplicationcontext.rootWEBApplication context is used when establishing Dispatcherservlet's own IOC contexts Attribute first gets the previous root context (that is, Webapplicationcontext) from ServletContext as the parent context of its own context. Once you have this parent context, initialize the context that you hold. The work of this dispatcherservlet initializing its own context can be seen in its Initstrategies method, which is about initializing processor mappings, view parsing, and so on. The default implementation class for the context that the servlet holds itself is also mlwebapplicationcontext. After initialization, spring is associated with the name of the servlet (not simply a servlet named key, but through some transformations, the specific self-view source) property is the property key, and it is stored in ServletContext for later use. Each servlet thus holds its own context, which has its own separate bean space, while each servlet shares the same bean, which is defined by the root context (the context initialized in step 2nd).
//------------------------------------
Yang2yang
Links: https://www.jianshu.com/p/5226c7fec43a
Source: Pinterest
Spring START process