public class ContextLoaderListener extends ContextLoader implements ServletContextListener{}
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()); }
主要用於建立WebApplicationContext;繼承
能夠在經過web.xml 的initParam來指定默認的WebApplicationContext的實現類就會採用XmlWebApplicationContext來當作默認;WebApplicationContext是存儲在ServletContext中的應用級變量;而且是惟一的!接口
在建立過程:
生命週期
1,得到加載類 CurrTheadClassLoader 或者 ClassUtils.class.getClassLoader(); 或者 ClassLoader.getSystemClassLoader();
2,開始經過加載類得到Class
3,經過Class的反射構造 newInstance