码迷,mamicode.com
首页 > Web开发 > 详细

WEB容器监听器详解 ServletContextListener

时间:2014-07-26 15:27:51      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:des   blog   java   使用   io   2014   for   art   

WEB容器监听器ServletContextListener主要用来监听容器启动和 销毁的时候需要做一些操作,就可以使用这个监听器来做。

ServletContextListener在Spring启动前启动。


我们实现一个简单的监听器,需要继承接口ServletContextListener:

 * 一个测试的监听器例子
 * @author zhuli
 * @date 2014-7-26
 */
public class TestContextLister implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("==============================容器装载");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("==============================容器销毁");
    }

}

ServletContextListener 实现两个接口,一个是容器启动的时候,一个是容器销毁的时候:

public interface ServletContextListener extends EventListener {
	/**
	 ** Notification that the web application initialization
	 ** process is starting.
	 ** All ServletContextListeners are notified of context
	 ** initialization before any filter or servlet in the web
	 ** application is initialized.
	 */

    public void contextInitialized ( ServletContextEvent sce );

	/**
	 ** Notification that the servlet context is about to be shut down.
	 ** All servlets and filters have been destroy()ed before any
	 ** ServletContextListeners are notified of context
	 ** destruction.
	 */
    public void contextDestroyed ( ServletContextEvent sce );
}

在web.xml中的配置:

	<listener>
		<listener-class>com.xxx.controller.web.TestContextLister</listener-class>
	</listener>

容器启动后,会在容器启动的日志中看到:

==============================容器装载
2014-07-26 08:54:01.302:INFO:/:Initializing Spring FrameworkServlet 'apiServlet'



WEB容器监听器详解 ServletContextListener,布布扣,bubuko.com

WEB容器监听器详解 ServletContextListener

标签:des   blog   java   使用   io   2014   for   art   

原文地址:http://blog.csdn.net/initphp/article/details/38139643

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!