"Implementation of the spring source--IOC container" (i) launch of the Web container

Source: Internet
Author: User
Tags deprecated
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.