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

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

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. Erlang虚拟机的启动

    Erlang虚拟机的启动 erl实际上是一个shell脚本,设置几个环境变量之后,调用执行erlexec.erlexec的入口点在 otp_src_R15B01/erts/etc/common/erl ...

  2. Java错误提示is not an enclosing class

    今天脑袋晕乎乎的,犯了个低级错误,好半天才反应过来 一直提示:is not an enclosing class 我居然把 RegisterActivity.class 写成了 RegisterAct ...

  3. 在sublime_text3中实现项目的跳转

    作为学习前端的小白,选择了sublime_text3作为学习的编译器.学习的过程是艰辛的,但也是快乐的.遇到自己不会的,有时候会折腾好几个小时,在实现预期效果的时候,那种兴奋真的难以言述. 今天,在学 ...

  4. 学习smail注入遇到的坑

    1.将需要被反编译的apk包解开之后,找到MainActivity,然后在OnCreate中添加需要加入注入的smail代码: Java代码: /** * 获取Android id * * @para ...

  5. [DP之计数DP]

    其实说实在 我在写这篇博客的时候 才刚刚草了一道这样类型的题 之前几乎没有接触过 接触过也是平时比赛的 没有系统的做过 可以说0基础 我所理解的计数dp就是想办法去达到它要的目的 而且一定要非常劲非常 ...

  6. [Jmeter]jmeter之初体验(windows下的jmeter)

    一.环境准备 1.安装JDK(传送门:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.ht ...

  7. Scala内部类

    注意:Java内部类从属于外部类,而Scala内部类从属于对象(外部类的实例本身).

  8. ECOS-LNMP ZendGuard

    因为ECOS产品加密后依赖PHP Zendguard运行(基于PHP5.3加密)  备注: Shopex485商城基于php5.2进行加密 URL:http://www.zend.com/en/pro ...

  9. C#模板打印excel

    using Microsoft.Office.Interop.Excel; //引用   public void PrintPriviewExcelFile(string filePath) {    ...

  10. 设置session存储在int sqlserver上---使用aspnet_regsql.exe工具

    以管理员身份打开命令窗 1)cd到相应的framework下,如:C:\Windows\Microsoft.NET\Framework\v4.0.30319 2)执行如下命令:aspnet_regsq ...