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

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

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. 数据集下载 MNIST

    The Street View House Numbers (SVHN) Dataset SVHN is a real-world image dataset for developing machi ...

  2. HDU 5880 Family View

    $AC$自动机. 用$AC$自动机匹配一次,开一个$flag$记录一下以$i$位置为结尾的最长要打$*$多少个字符,然后倒着扫描一次就可以输出了. #pragma comment(linker, &q ...

  3. PHP学习资源

    PHPerNote PHPerNote 是一个php程序员的工作生活笔记,本站包含了php网络编程学习教程,数据库(主要是MySQL数据库)教程,javascript,jquery,div+css,h ...

  4. iOS开发系列-UI基础-KVC

    这些知识是UI初级学习的,目前我还在学习中,适合初学者看 KVC—Key Value Coding 也就是键值编码 是一种获取值和设置值的方式 当我们创建一个类文件,为这个类设置成员属性的时候: 创建 ...

  5. [M]MagicTable转换异常解决方法

    ApplicationClass转换为_Application异常 这个问题可能是以前安装过不同版本的Office,没有完全卸载时导致的,可尝试通过以下方法解决: 1. 卸载Office,在 控制面板 ...

  6. window 7 安装mysql

    Mysql版本5.7.16 下载完mysql包之后,直接解压缩. 编辑my-default.ini文件,将下面2行注释取消. basedir = e:/mysql #mysql解压目录 datadir ...

  7. PHP字符串转义

    与PHP字符串转义相关的配置和函数如下: 1.magic_quotes_runtime 2.magic_quotes_gpc 3.addslashes()和stripslashes() 4.mysql ...

  8. github上前100的ios项目

    1. AFNetworking    作者是 NSHipster 的博主, iOS 开发界的大神级人物, 毕业于卡内基·梅隆大学, 开源了许多牛逼的项目, 这个便是其中之一, AFNetworking ...

  9. UIPickerView 创建中国地区显示 省份 市

    第一步初始化 UIPickerView *pickerview; //城市滚动表 NSDictionary *dict; //用于存储省份-城市的数据 NSArray *provinceArray; ...

  10. Zabbix 监控rabbitmq

    一 应用场景描述 测试环境业务 OpenStack中搭建了rabbitmq作为RPC消息队列,所以mq起到了很重要的角色,本案例是来如何使用zabbix监控 rabbitmq业务 二 Rabbitmq ...