Preface1. Since you normally use spring as a basic Web project, today is a Web perspective on how the IOC container starts and manages beans.
2. This article and subsequent code version: Spring3.0.5. So if you find code (or diagram) inconsistencies, notice the spring version.
3. It is recommended that you read the source code in the first few times, the first way to pass, and then back to study the details.
Web Container Interface System
To facilitate the use of IOC containers in a Web environment, Spring provides a context interface Webapplicationcontext for Web applications to meet the needs of the startup process. The class-level relationship of this Webapplicationcontext interface is shown in the figure:
In the interface design, finally through the ApplicationContext interface and Beanfactory interface docking, and for the realization of specific functions, Many of them are encapsulated in Abstractrefreshablewebapplicationcontext. And look at this inheritance system from a code point of view: configuration in the startup 1.web.xml of the Web container
In order to use spring, we typically match a listener in Web.xml. As follows:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath: applicationcontext.xml</param-value> </context-param> <listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener>
So the entry of the container launch is here, context-param literal meaning can be understood, is the ServletContext parameter, here we have configured an XML path. We focus on listener, this spring's Contextloaderlistener implements the Servletcontextlistener interface, which is defined in the Servlet API, Provides callbacks that are combined with the servlet lifecycle, such as contextinitialized and Contextdestroy. Then, as a ServletContext listener, if the ServletContext changes, the listener will make the appropriate event. For example, when a server (tomcat, etc) is started, the Servletcontextlistener contextinitialized method is invoked and the Contextdestroy method is invoked when the server is about to close. Let's take a look at the Contextloaderlistener code:
Code 1.1:contextloaderlistener Source
public class Contextloaderlistener extends Contextloader implements Servletcontextlistener {private Contextloader conte Xtloader; /** * Initializes the container * Initialize the root Web application context. */public void contextinitialized (Servletcontextevent event) {// This method is intended to provide a placeholder method Createcontextloader () to create a customized environment for the subclass opportunity to load, but later this proof is not very useful, has been encouraged to no longer use, in fact, subclasses can be overridden by this method to achieve the same purpose This.contextloader = Createcontextloader (); if (This.contextloader = = null) {This.contextloader = this;} this.contextLoader.initWebApplicationContext ( Event.getservletcontext ()); /** * Create the Contextloader to use. Can is overridden in subclasses. * @return The new contextloader * @deprecated in favor of Simply subclassing Contextloaderlistener itself * (which extends Contextloader, as of Spring 3.0) */@Deprecated protected Contextloader Createcontextloader () {return null;}/** * Retu RN The Contextloader used by this listener. * @return The current Contextloader * @deprecated in favor of simply subclassing ContextloaderlisTener itself * (which extends Contextloader, as of Spring 3.0) */@Deprecated public contextloader Getcontextloader ()