在Servlet API中有一個ServletContextListener接口,它能夠監聽ServletContext對象的生命周期,實際上就是監聽Web應用的生命周期。
當Servlet容器啟動或終止Web應用時,會觸發ServletContextEvent事件,該事件由 ServletContextListener 來處理。在 ServletContextListener 接口中定義了處理ServletContextEvent事件的兩個方法。
l contextInitialized(ServletContextEvent sce) :當Servlet 容器啟動Web 應用時調用該方法。在調用完該方法之后,容器再對Filter 初始化,并且對那些在Web 應用啟動時就需要被初始化的Servlet 進行初始化。
l contextDestroyed(ServletContextEvent sce) :當Servlet 容器終止Web 應用時調用該方法。在調用該方法之前,容器會先銷毀所有的Servlet 和Filter 過濾器。
SPRing 入口類ContextLoaderListener 函數實例
/** * Initialize the root web application context. */ @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); } /** * Close the root web application context. */ @Override public void contextDestroyed(ServletContextEvent event) { closeWebApplicationContext(event.getServletContext()); ContextCleanupListener.cleanupAttributes(event.getServletContext()); }在xml中的配置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>參考:http://blog.csdn.net/chang_li/article/details/51915756
http://blog.csdn.net/zhaozheng7758/article/details/6103700
http://www.cnblogs.com/HigginCui/p/5970186.html
新聞熱點
疑難解答