Spring源码1——从启动ContextLoaderListener看Spring

ContextLoaderListener的定义:

public class ContextLoaderListener extends ContextLoader implements ServletContextListener{}

  

   1,实现的接口   ServletContextListenter

    ServletContextListenter是J2ee Servlet标准的一部分,用于监听ServletContext的启动和销毁;java

public interface ServletContextListener extends EventListener {

    /**
     * Receives notification that the web application initialization
     * process is starting.
     *
     * <p>All ServletContextListeners are notified of context
     * initialization before any filters or servlets in the web
     * application are initialized.
     *
     * @param sce the ServletContextEvent containing the ServletContext
     * that is being initialized
     */
    public void contextInitialized(ServletContextEvent sce);

    /**
     * Receives notification that the ServletContext is about to be
     * shut down.
     *
     * <p>All servlets and filters will have been destroyed before any
     * ServletContextListeners are notified of context
     * destruction.
     *
     * @param sce the ServletContextEvent containing the ServletContext
     * that is being destroyed
     */
    public void contextDestroyed(ServletContextEvent sce);
}

    EventListener是事件标志接口web

/**
 * A tagging interface that all event listener interfaces must extend.
 * @since JDK1.1
 */
public interface EventListener {
}

    Servlet生命周期
app

        Web容器启动   加载Web.xml中配置的Servlet  第一次开始调用   开始应用期间的一次初始化;   当应用中止  Servlet调用Destoryed;ide

    ServletContextspa

         表明着web应用一个应用只有一个记录整个应用的信息;
code

    总结:ContextLoadListener实现ServletContextListener是为了在应用启动或者关闭时作一些必要的工做;xml

/**
	 * 重写了ServletContext初始化监听
	 * Initialize the root web application context.
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		initWebApplicationContext(event.getServletContext());
	}


	/**
	 *重写了ServletContext销毁监听
	 * Close the root web application context.
	 */
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		closeWebApplicationContext(event.getServletContext());
		ContextCleanupListener.cleanupAttributes(event.getServletContext());
	}


    2,继承ContextLoader

        主要用于建立WebApplicationContext;继承

        能够在经过web.xml 的initParam来指定默认的WebApplicationContext的实现类就会采用XmlWebApplicationContext来当作默认;WebApplicationContext是存储在ServletContext中的应用级变量;而且是惟一的!接口

        在建立过程:
生命周期

                    1,得到加载类   CurrTheadClassLoader  或者 ClassUtils.class.getClassLoader();  或者   ClassLoader.getSystemClassLoader();

                    2,开始经过加载类得到Class

                    3,经过Class的反射构造   newInstance

相关文章
相关标签/搜索