ApplicationContext is the core of spring, which we usually interpret as context, I want to use "container" to express it easier to understand some, ApplicationContext is "the container of Application", in the Web application, We'll use Webapplicationcontext,webapplicationcontext to inherit from ApplicationContext. The initialization of Webapplicationcontext is different from Beanfactory.applicationcontext because Webapplicationcontext needs to ServletContext instances, This means that it must have a web container in order to complete the startup work. Readers with web development experience know that you can configure a self-initiated servlet or a definition Web Container listener (servletcontextlistener) in XML. With either of these, we can start the work of the Spring Web application context.
Spring provides a servlet and Web container listener for starting Webapplicationcontext, respectively:
Org.springframework.web.context.ContextLoaderServlet;
Org.springframework.web.context.ContextLoaderListener.
Both of these methods initialize the Webapplicationcontext when the Web App is launched, and I personally think that Listerner is better than the servlet because Listerner listens to the start and end of the application, And the servlet has to be started slightly delayed some, if at this time to do some business operations, the pre-and post-activation sequence is affected.
The
Configuration example is as follows:
context-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/applicationcontext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.contextloaderlistener </listener-class>
</listener>
What exactly did you do in Contextloaderlistener and Contextloaderservlet?
Take Contextloaderlistener as an example, we can see
public void contextinitialized (Servletcontextevent event) {
This.contextloader = Createcontextloader ();
This.contextLoader.initWebApplicationContext ( Event.getservletcontext ());
}
protected Contextloader Createcontextloader () {
return New Contextloader ();
}
Contextloader is a tool class used to initialize Webapplicationcontext, The main method is Initwebapplicationcontext, we continue to track Initwebapplicationcontext This method (the specific code I do not post, we can see the source in spring), we found that The original Contextloader is to put Webapplicationcontext (Xmlwebapplicationcontext is the default implementation class) in the ServletContext, ServletContext is also a "container "is also a map-like structure, and Webapplicationcontext's key in ServletContext is Webapplicationcontext.root_web_application_context_ ATTRIBUTE, if we want to use webapplicationcontext, we need to take the ServletContext out, Spring provides a webapplicationcontextutils class, Can be easily removed webapplicationcontext, as long as the ServletContext incoming on it.
1 webapplicationcontext ctx=webapplicationcontextutils.getwebapplicationcontext (sevletcontext);
Xml
<Listener> <Listener-class> Org.springframework.web.context.request.RequestContextListener </Listener-class></Listener>
Applicationcontext.xml
<?xml version= "1.0" encoding= "GBK"? ><web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi= "http:/ /www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org /xml/ns/javaee/web-app_3_1.xsd "version=" 3.1 "> <!--configuring Beans in Applicationcontext.xml-><!--< Bean id= "" class= "" scope= "requst/session"/> --></beans>
About the initialization of Webapplicationcontext