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. windows驱动开发-设备扩展

    设备对象Device_Object记录通用设备信息,另外一些信息记录在设备扩展里,设备扩展由程序员自己定义,由程序员指定内容和大小,由I/O管理器创建,并保存在非分页内存中. 驱动程序中,尽量避免使用 ...

  2. 多线程分析之Semaphore

    Semaphore分析由来 网上看了许多讲解Semaphore的,用Semaphore来实现顺序打印字母,但是可能大家都没有清楚具体的原因,所以来给大家分析下为什么可以使用Semaphore来实现顺序 ...

  3. day17-Python运维开发基础(类的封装 / 对象和类的相关操作、构造方法)

    1. 类的封装及相关操作 # ### oop 面向对象程序开发 """ #用几大特征表达一类事物称为一个类,类更像是一张图纸,表达的是一个抽象概念 "" ...

  4. Lognormal distribution 对数正态分布

    转载:https://blog.csdn.net/donggui8650/article/details/101556041 在概率论中,对数正态分布是一种连续概率分布,其随机变量的对数服从正态分布. ...

  5. PHP 获取header 的自定义参数值

    $.ajax({ type: "GET", url: "default.aspx", beforeSend: function(request) { reque ...

  6. Thymeleaf--起步

    Spring官方支持的服务的渲染模板中,并不包含jsp.而是Thymeleaf和Freemarker等,而Thymeleaf与SpringMVC的视图技术,及SpringBoot的自动化配置集成非常完 ...

  7. 「NOIP2011」聪明的质监员

    传送门 Luogu 解题思路 第一眼肯定是没什么思路的 dalao勿喷,但我们仔细看一看式子就会发现 \(Y\) 是随着 \(W\) 的变大而变小的. 所以 \(Y\) 随 \(W\) 的变化是单调的 ...

  8. MyEclipse 8.5整合Git,并在Github上发布项目

    我们在闲暇时间想加入些团队做点属于自己有意义的东西,那Github就是为你准备的.但是用惯SVN的我们就得学习学习了. 工具/原料 myeclipse8.5 github 方法/步骤 1 下载Ecli ...

  9. LINQ---查询表达式的结构

    重要事项: 子句必须按照一定的顺序出现 from子句和select...group子句这两部分是必须的 其他子句是可选的 在LINQ查询表达式中,select子句在表达式最后. 可以后任意多的from ...

  10. 从零到Django大牛的的进阶之路02

    Cookie/Session Cookie Cookie以键值对的格式进行信息的存储. Cookie基于域名安全,不同域名的Cookie是不能互相访问的,如访问itcast.cn时向浏览器中写了Coo ...