//事件涉及的三个组件:事件源、事件对象、事件监听器

//一个总的事件监听器接口,所有不同分类的事件监听器都继承这个接口

public interface EventListener {
}

//例如 ServletContextListener 这个ServletContext监听器  他有两个方法,一个是初始化方法,另一个是销毁的方法。在web.xml中配置了listener,那么容器在启动时就会扫描web.xml的listener的配置,自动调用contextInitialized 方法,在容器重启时,他会先调用contextDestroyed销毁的方法,在调用contextInitialized 创建的方法。在容器关闭时,会调用contextDestroyed方法。但我刚启动完容器,立刻关闭,是不会调用到contextDestroyed方法的。我想他这里应该有设置时间的吧。

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 ); //接受的参数是ServletContextEvent 事件源
/** ** 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 ); }

//每一个事件对象都有一个总的事件对象类,下面这个就是了。

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 + "]";
}
}
public class ServletContextEvent extends java.util.EventObject { 

    /** Construct a ServletContextEvent from the given context.
*
* @param source - the ServletContext that is sending the event.
*/
public ServletContextEvent(ServletContext source) {
super(source);
} /**
* Return the ServletContext that changed.
*
* @return the ServletContext that sent the event.
*/
public ServletContext getServletContext () {
return (ServletContext) super.getSource();//返回一个真正的事件源
} }

这就是tomcat的事件监听设计模型。

tomcat的事件监听的更多相关文章

  1. SpringBoot入门之事件监听

    spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利,sptingboot支持的事件类型有以下五种: ApplicationStartingEvent Applicatio ...

  2. Java设计模式——观察者模式(事件监听)

    最近在看Tomcat和Spring的源码,在启动的时候注册了各种Listener,事件触发的时候就执行,这里就用到了设计模式中的观察者模式. 引-GUI中的事件监听 想想以前在学Java的GUI编程的 ...

  3. 7_3.springboot2.x启动配置原理_3.事件监听机制

    事件监听机制配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListenerioc容器中的 ...

  4. SpringBoot Application事件监听

    SpringBoot Application共支持6种事件监听,按顺序分别是: ApplicationStartingEvent:在Spring最开始启动的时候触发 ApplicationEnviro ...

  5. springBoot高级:自动配置分析,事件监听,启动流程分析,监控,部署

    知识点梳理 课堂讲义 02-SpringBoot自动配置-@Conditional使用 Condition是Spring4.0后引入的条件化配置接口,通过实现Condition接口可以完成有条件的加载 ...

  6. Java中用得比较顺手的事件监听

    第一次听说监听是三年前,做一个webGIS的项目,当时对Listener的印象就是个"监视器",监视着界面的一举一动,一有动静就触发对应的响应. 一.概述 通过对界面的某一或某些操 ...

  7. 4.JAVA之GUI编程事件监听机制

    事件监听机制的特点: 1.事件源 2.事件 3.监听器 4.事件处理 事件源:就是awt包或者swing包中的那些图形用户界面组件.(如:按钮) 事件:每一个事件源都有自己特点有的对应事件和共性事件. ...

  8. Node.js 教程 05 - EventEmitter(事件监听/发射器 )

    目录: 前言 Node.js事件驱动介绍 Node.js事件 注册并发射自定义Node.js事件 EventEmitter介绍 EventEmitter常用的API error事件 继承EventEm ...

  9. .NET事件监听机制的局限与扩展

    .NET中把“事件”看作一个基本的编程概念,并提供了非常优美的语法支持,对比如下C#和Java代码可以看出两种语言设计思想之间的差异. // C#someButton.Click += OnSomeB ...

随机推荐

  1. Hybrid App(二)Cordova+android入门

    上一篇介绍了app开发如何选型,由于公司人员组织结构的原因(app native较少,每个月一次迭代),因此选型hybrid app. 接下来说一下环境搭建: 一.Java环境 (1)安装jdk (2 ...

  2. 关于IIS和.NET 4.0的问题总结(转)

    注册asp.net 4.0 到iis   如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下 运行->cmd Microsoft Windows [版本 6.1.7601 ...

  3. [SQL基础教程] 2-3 逻辑运算符

    [SQL基础教程] 2-3 逻辑运算符 NOT AND OR 优先级 ( )改变优先级 AND 优先级高于 OR NULL 引入三值逻辑

  4. Office下载地址

    文件名cn_office_professional_plus_2016_x86_x64_dvd_6969182.isoSHA1277926A41B472EE38CA0B36ED8F2696356DCC ...

  5. 一个3D视频播放器的演示APK

    介绍: 这个APK是把视频显示分割成左右对等的两幅画面.同时无缝显示在屏幕上, 配合类似谷歌的cartdboard "纸片壳" 或市面上的魔镜等3D眼镜来播放视频画面, 根据3D眼 ...

  6. 一.HttpClient、JsonPath、JsonObject运用

    HttpClient详细应用请参考官方api文档:http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/apidocs/index.h ...

  7. child_process小解

    js是一种单进程单线程的语言,但现行的cpu都是多核的,为了解决单进程单线程对多核使用不足的问题,child_process应运而生,理想情况下每个进程各自利用一个内核. 主要有四种方法来创建子进程, ...

  8. 【c++】size_t 和 size_type的区别

    为了使自己的程序有很好的移植性,c++程序员应该尽量使用size_t和size_type而不是int, unsigned 1. size_t是全局定义的类型:size_type是STL类中定义的类型属 ...

  9. 重读The C programming Lanuage 笔记三:简单计算器程序

    //简单计算器 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <str ...

  10. SpringMVC初步——HelloWorld的实现

    开通博客园好几个月了,今天开始要用博客园记录自己的学习过程! 目录: 导包: 1. 配置web.xml文件的springDispatcherServlet 在xml中 alt+/ 找到springdi ...