您的位置:首页 > 编程语言 > Java开发

Java彻底 - WEB容器的侦听具体解释 ServletContextListener

2015-07-04 14:20 525 查看
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'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: