Spring 系列教程


除了内置事件,Spring中也可以使用自定义事件。

怎样使用自定义事件:

  • 创建事件类 - 扩展ApplicationEvent类,创建事件类。
  • 创建发送类 - 发送类获取ApplicationEventPublisher实例发送事件。
  • 创建监听类 - 实现ApplicationListener接口,创建监听类。

事件类

事件类用于存储事件数据。下面创建一个简单的事件类。

CustomEvent.java

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {

    public CustomEvent(Object source, String message) {
super(source);
} public String toString() {
return "我是自定义事件";
}
}

发送类

发送类创建事件对象并发送。

要发送事件,这里介绍2种方法:

  1. 使用@autowired注解注入ApplicationEventPublisher实例。

CustomEventPublisher.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher; public class CustomEventPublisher { @Autowired
private ApplicationEventPublisher publisher; public void publish() {
CustomEvent event = new CustomEvent(this);
publisher.publishEvent(event);
}
}
  1. 发送类实现ApplicationEventPublisherAware接口,获取ApplicationEventPublisher实例。

CustomEventPublisher.java

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware; public class CustomEventPublisher implements ApplicationEventPublisherAware { private ApplicationEventPublisher publisher; // 必须重写这个方法获取ApplicationEventPublisher
public void setApplicationEventPublisher (ApplicationEventPublisher publisher){
this.publisher = publisher;
} public void publish() {
CustomEvent event = new CustomEvent(this);
publisher.publishEvent(event);
}
}

如果发送类实现了ApplicationEventPublisherAware接口,发送类必须声明为bean,Spring容器将其标识为事件发送者。

<bean id="customEventPublisher" class="CustomEventPublisher"/>

监听类

监听类监听事件。监听类必须实现ApplicationListener接口,并且被定义为Bean以便Spring容器可以加载它。

beans.xml

<bean id="customEventHandler" class="CustomEventHandler"/>

CustomEventHandler.java

import org.springframework.context.ApplicationListener;

public class CustomEventHandler implements ApplicationListener<CustomEvent> {
public void onApplicationEvent(CustomEvent event) {
System.out.println("收到事件:" + event.toString());
}
}

运行

测试自定义事件。

Test.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); CustomEventPublisher publisher = (CustomEventPublisher) context.getBean("customEventPublisher");
publisher.publish();
}
}

Spring 事件(2)- 自定义事件的更多相关文章

  1. javaScript事件机制深入学习(事件冒泡,事件捕获,事件绑定方式,移除事件方式,阻止浏览器默认行为,事件委托,模拟浏览器事件,自定义事件)

    前言 JavaScript与HTML之间的交互是通过事件实现的.事件,就是文档或浏览器窗口中发生的一些特定的交互瞬间.可以使用侦听器(或处理程序)来预订事件,以便事件发生时执行相应的代码.这种在传统软 ...

  2. jQuery-3.事件篇---自定义事件

    jQuery自定义事件之trigger事件 众所周知类似于mousedown.click.keydown等等这类型的事件都是浏览器提供的,通俗叫原生事件,这类型的事件是需要有交互行为才能被触发. 在j ...

  3. jQuery基础(鼠标事件,表单事件,键盘事件,自定义事件 篇)

    1.jQuery鼠标事件之click与dbclick事件   方法一:$ele.click()(不带参数)   <div id="test">点击触发<div&g ...

  4. 63.ExtJs事件(自定义事件、on、eventManager)示例

    转自:https://blog.csdn.net/leadergg/article/details/5927614?utm_source=blogxgwz5 ExtJs事件(自定义事件.on.even ...

  5. Spring容器事件、自定义事件

    Spring容器内置事件,如容器的启动.停止.关闭.销毁等事件 <bean name="contextStartedHandler" class="com.nuts ...

  6. Spring中实现自定义事件

    原理: 通过扩展ApplicationEvent,创建一个事件类CustomEvent.这个类必须定义一个默认的构造函数,它应该从ApplicationEvent类中继承的构造函数. 一旦定义事件类, ...

  7. Spring(十)之自定义事件

    编写自定义事件的简单流程如下: (1)编写CustomEvent.java package com.tutorialspoint; import org.springframework.context ...

  8. jQuery on() 方法 为选定已存在元素和未来元素绑定标准事件和自定义事件

    很有必要说说jQuery的on方法,这个方法存在大乾坤大奥秘,主要注意两点: 1.为已存在元素和未来元素(动态添加元素)绑定处理函数. 2.自定义一个非标准的事件并绑定处理函数. 定义和用法 on() ...

  9. Angular4.x Event (DOM事件和自定义事件)

    Angular组件和DOM元素通过事件与外部进行通信,两者中的事件绑定语法是相同的-(eventName)="expression": <button (click)=&qu ...

  10. jQuery事件命名空间多事件绑定自定义事件js 命名空间 javascript命名空间

    http://blog.csdn.net/pigpigpig4587/article/details/24727791 jQuery事件命名空间 jQuery支持事件命名空间,以方便事件管理.例如,在 ...

随机推荐

  1. 全球定位IP位置 2018(离线版)

    球定位IP位置 2018(离线版) 这次写的软件使用Python写的,所以体积可能有点大 我特地写了GUI打包成了Exe可执行文件,方便小白使用== 只要输入目标ip就能显示目标所在的国家城市和经纬度 ...

  2. 在 Mac/Windows 系统中使用 Laradock 搭建基于 Docker 的 Laravel 开发环境 (改)

    开篇 Use Docker First And Learn About It Later 简介 Laradock 是为 Docker 提供的完整 PHP 本地开发环境,和 Homestead 一样提供 ...

  3. C#委托-事件示例

    Program.cs using System;using System.Collections.Generic;using System.Linq;using System.Text;using S ...

  4. SSH框架搭建 笔记 (含spring注解驱动)

    分类: web 开发2014-04-27 12:33 354人阅读 评论(0) 收藏 举报 框架springinterface注解 好久没有搭建框架了,今天整理下以前的知识,整合下SSH,没想到手生了 ...

  5. wx.previewimage预览返回会触发onshow的处理方法

    最近做详情页,添加图片预览后竟然触发onshow的处理方法.就显得很尴尬.框架用的uni-app 解决方法 1.page外全局定义开关变量 var a; export default { } 2 .o ...

  6. vscode修改样式

    以修改上方滚动条宽度为例 打开开发者工具 help->toggle developer tool 或者快捷键 ctrl+shift+i 选择滚动条,找到css对应文件 鼠标移上去可以看到路径,类 ...

  7. SChema 多个属性的设置学习

    <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http:/ ...

  8. HihoCoder第四周:Trie图

    第四周的题目是前两周的综合,综合在一个是KMP算法的思想,一个是树的这么一个数据结构. 题目 : Trie图 输入 每个输入文件有且仅有一组测试数据. 每个测试数据的第一行为一个整数N,表示河蟹词典的 ...

  9. Oracle 的DBA考证

    转自 :https://www.cnblogs.com/chunge2050/archive/2013/04/16/3023730.html 详细的了解了几天之后,总结起来就是oracle为DBA认证 ...

  10. nested exception is javax.management.InstanceAlreadyExistsException: webservice:name=statFilter,type=StatFilter

    Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [ ...