WebApplicationContext initialization

 

 ApplicationContext is the core of Spring. We usually interpret Context as context. I want to use "container" to describe it to be easier to understand. ApplicationContext is "application container"; in web applications, we will use WebApplicationContext, WebApplicationContext Inherited from ApplicationContext; the initialization method of WebApplicationContext is different from BeanFactory.ApplicationContext, because WebApplicationContext requires a ServletContext instance, that is to say, it must have a Web container to complete the startup work. Readers who have experience in Web development know that you can Configure the self-starting Servlet in web.xml or define the Web container listener (ServletContextListener), with either of the two, we can start the work of the Spring Web application context.

Spring has three startup methods, using ContextLoaderServlet, ContextLoaderListener and ContextLoaderPlugIn
spring 3.0 and later versions have been deleted ContextLoaderServlet and Log4jConfigServlet
can use the remaining two startup methods ContextLoaderListener and ContextLoaderPlugIn search
recommend using ContextLoaderListener

These two methods are to initialize the WebApplicationContext when the web application starts. I personally think that the Listener is better than the Servlet, because the Listener monitors the start and end of the application, and the Servlet has to start a little delay. If you want to When doing some business operations, the sequence of startup has an impact.

配置例子如下:
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> 

So what exactly is done in ContextLoaderListener and ContextLoaderServlet? 
Taking ContextLoaderListener as an example, we can see that 
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. Its main method is initWebApplicationContext. We continue to track the method of initWebApplicationContext (I will not post the specific code, you can see the source code in Spring). We found that the original ContextLoader is the WebApplicationContext (XmlWebApplicationContext is the default implementation class) is placed in ServletContext, ServletContext is also a "container" and a structure similar to Map, and the KEY of WebApplicationContext in ServletContext is WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, if we want to use WebApplicationContext, we need to take it out from ServletContext, Spring provides With a WebApplicationContextUtils class, you can easily take out the WebApplicationContext, as long as you pass in the ServletContext. 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326693280&siteId=291194637