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. 常用WinAPI函数整理------------转载

    常用WinAPI函数整理原创 玩撕你 发布于2019-09-04 20:06:55 阅读数 101 收藏展开 之前的博客写了很多关于Windows编程的内容,在Windows环境下的黑客必须熟练掌握底 ...

  2. 与Python的第一次见面

    1.Python的起源 Python的作者,Guido von Rossum,确实是荷兰人.1982年,Guido从阿姆斯特丹大学(University of Amsterdam)获得了数学和计算机硕 ...

  3. MyEclipse插件github安装使用

    MyEclipse插件github安装使用   网络上的介绍一堆堆的,但是自己尝试了下,发现问题很多,就动手做个教程. 大纲 1.git客户端安装 2.ssh配置 3.egit安装配置 4.参考资料 ...

  4. call 和 apply 用法

    ECMAScript规范中,所有函数都包含这两个方法,并且两个方法的使用基本一致,都是用于改变函数的作用域,即改变函数体内 this 指向.不同的是 call 方法的第二个参数可以接收任意个参数,以逗 ...

  5. PostgreSQL数据库-抽奖sql

    select * from users order by random();--会进行随机排序,每次运行都会返回不同的结果 select * from users order by random() ...

  6. STM32+Nokia5110LCD

    Nokia5110LCD(84*48) lcd.h #ifndef _LCD_H#define _LCD_H #include "sys.h" #include "std ...

  7. SpringBoot如何返回页面

    SpringBoot中使用Controller和页面的结合能够很好地实现用户的功能及页面数据的传递.但是在返回页面的时候竟然会出现404或者500的错误,我总结了一下如何实现页面的返回以及这里面所包含 ...

  8. Day4 - G - 确定比赛名次 HDU - 1285

    有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道 ...

  9. angularJS MVC及$scope作用域

  10. 创建springboot2.1项目运行报错

    刚创建好一个项目,运行就报错:in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging ...