Spring源码解读:ServletContextListener接口

xiaoxiao2021-02-28  137

ServletContextListener接口的源码

package javax.servlet; import java.util.EventListener; /** * Implementations of this interface receive notifications about * changes to the servlet context of the web application they are * part of. * To receive notification events, the implementation class * must be configured in the deployment descriptor for the web * application. * @see ServletContextEvent * @since v 2.3 */ 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 ); } 此接口被EventListener标记,用来接收web应用中servlet 上下文变化的通知,同时,为了接收通知事件。该 具体实现类必须在web.xml中配置(部署描述符)。

其中设计了两个方法:

//通知web应用的初始化处理开始,所有的ServletContextListeners在filter or servlet在(web应用)初始化之前都会被接收到上下文初始化的信息。

public void contextInitialized ( ServletContextEvent sce );

//在上下文中的销毁中,所有的filter or servlet在销毁之间ServletContextListeners都会被提前通知。

 public void contextDestroyed ( ServletContextEvent sce );

其中的实现类有:

同时该接口引入了ServletContextEvent事件类:

ServletContextEvent:这是关于Web应用程序的servlet上下文更改的通知的事件类。

package javax.servlet; /** * This is the event class for notifications about changes to * the servlet context of a web application. * @see ServletContextListener * @since v 2.3 */ public class ServletContextEvent extends java.util.EventObject { /** 从给定的上下文中构建一个ServletContextEvent对象 * * @param source - the ServletContext that is sending the event. */ public ServletContextEvent(ServletContext source) { super(source); } /** * 返回一个Servlet上下对象 * * @return the ServletContext that sent the event. */ public ServletContext getServletContext () { return (ServletContext) super.getSource(); } } 其中有EventObject和ServletContext。

对于EventObject没啥特别的就是jdk工具类提供的一个事件对象,获取对象只能通过getSource返回一个Object。

ServletContextEvent也使用相同的手段,只能通过传入一个ServletContext构建一个对象,并返回ServletContext。

package java.util; /** * <p> * The root class from which all event state objects shall be derived. * <p> * All Events are constructed with a reference to the object, the "source", * that is logically deemed to be the object upon which the Event in question * initially occurred upon. * * @since JDK1.1 */ public class EventObject implements java.io.Serializable { private static final long serialVersionUID = 5516075349620653480L; /** * The object on which the Event initially occurred. */ protected transient Object source; /** * Constructs a prototypical Event. * * @param source The object on which the Event initially occurred. * @exception IllegalArgumentException if source is null. */ public EventObject(Object source) { if (source == null) throw new IllegalArgumentException("null source"); this.source = source; } /** * The object on which the Event initially occurred. * * @return The object on which the Event initially occurred. */ public Object getSource() { return source; } /** * Returns a String representation of this EventObject. * * @return A a String representation of this EventObject. */ public String toString() { return getClass().getName() + "[source=" + source + "]"; } } 对于ServletContext只是定义了一系列Servlet与Servlet容器通信的方法,例如获取一个文件的MIME的类型,分配一个请求或者写日志文件。

在java虚拟机中一个应用只有一个上下文ServletContext

转载请注明原文地址: https://www.6miu.com/read-18699.html

最新回复(0)