java 自定义监听_Spring 中的自定义事件
Spring 中的自定义事件
编写和发布自己的自定义事件有许多步骤。按照在这一章给出的说明来编写,发布和处理自定义 Spring 事件。
步骤
描述
1
创建一个名称为SpringExample的项目,并且在创建项目的src文件夹中创建一个包com.tutorialspoint。
2
使用AddExternalJARs选项,添加所需的Spring库,解释见SpringHelloWorldExample章节。
3
通过扩展ApplicationEvent,创建一个事件类CustomEvent。这个类必须定义一个默认的构造函数,它应该从ApplicationEvent类中继承的构造函数。
4
一旦定义事件类,你可以从任何类中发布它,假定EventClassPublisher实现了ApplicationEventPublisherAware。你还需要在XML配置文件中声明这个类作为一个bean,之所以容器可以识别bean作为事件发布者,是因为它实现了ApplicationEventPublisherAware接口。
5
发布的事件可以在一个类中被处理,假定EventClassHandler实现了ApplicationListener接口,而且实现了自定义事件的onApplicationEvent方法。
6
在src文件夹中创建bean的配置文件Beans.xml和MainApp类,它可以作为一个Spring应用程序来运行。
7
最后一步是创建的所有Java文件和Bean配置文件的内容,并运行应用程序,解释如下所示。
这个是 CustomEvent.java 文件的内容:
package com.tutorialspoint;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent{
public CustomEvent(Object source) {
super(source);
}
public String toString(){
return "My Custom Event";
}
}
下面是 CustomEventPublisher.java 文件的内容:
package com.tutorialspoint;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class CustomEventPublisher
implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
public void setApplicationEventPublisher
(ApplicationEventPublisher publisher){
this.publisher = publisher;
}
public void publish() {
CustomEvent ce = new CustomEvent(this);
publisher.publishEvent(ce);
}
}
下面是 CustomEventHandler.java 文件的内容:
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
public class CustomEventHandler
implements ApplicationListener{
public void onApplicationEvent(CustomEvent event) {
System.out.println(event.toString());
}
}
下面是 MainApp.java 文件的内容:
package com.tutorialspoint;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
cvp.publish();
cvp.publish();
}
}
下面是配置文件 Beans.xml:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
class="com.tutorialspoint.CustomEventHandler"/>
class="com.tutorialspoint.CustomEventPublisher"/>
一旦你完成了创建源和 bean 的配置文件后,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下信息:
My Custom Event
My Custom Event
java 自定义监听_Spring 中的自定义事件相关推荐
- android 实现自定义监听接口,Android在自定义类中实现自定义监听器方式
Android在自定义类中实现自定义监听器方式 发布时间:2020-08-31 06:19:39 来源:脚本之家 阅读:203 作者:Simon_Qi 监听器可以说是Android开发中最常用的东西之 ...
- java 自动启动监听_Spring Boot 启动事件和监听器,太强大了!
大家都知道,在 Spring 框架中事件和监听无处不在,打通了 Spring 框架的任督二脉,事件和监听也是 Spring 框架必学的核心知识之一. 一般来说,我们很少会使用到应用程序事件,但我们也不 ...
- java redis监听问题_springboot+redis过期事件监听实现过程解析
1 修改 redis.conf配置文件: K Keyspace events, published with keyspace@ prefix事件 E Keyevent events, publish ...
- java实时监听mysql_Java 中能实现监听数据库吗?
最近也在看这个.好像通过CDC实现对binlog的监控,然后实时进行数据传输或者更改.占个坑,等搞好这一块,有时间来回答 好,经过了一段时间的调研以及测试,我现在来回答一下这个问题. 通过CDC(ca ...
- Android 监听系统中消息通知事件
0. 学习文章 参考了下面Blog 完全没有任何多余的代码 https://blog.csdn.net/wanghang1208/article/details/49905403 原来百度卫士的通知栏 ...
- java键盘监听wasd控制_dota2rpg自定义按键绑定及WASD移动的实现
本文的目的是在DOTA2自定义游戏中实现一个WASD控制的8方向移动,如果你想实现的是4方向的,稍微修改一点点代码也可以做到. 通过本文,你也可以了解到DOTA2自定义按键的流程. 本文的所有代码你都 ...
- Android 自定义监听回调Listener
自定义监听回调Listener 使用场景:在某一页面布局中,除了原生View还有自定义View,当触发自定义View的击事件时,该页面的原生布局需要同步进行处理 demo:主页面有两部分UI,一部分是 ...
- Android自定义监听回调
我们常用的Button的点击事件,就是谷歌为我们封装好的监听回调,我们在调用setOnClickListener时就是在设置监听. 实际项目中我们也常常需要自定义监听回调,下面就是模仿setOnCli ...
- SparkListener监听使用方式及自定义的事件处理动作
本文针对spark 2.0+版本 概述 spark 提供了一系列整个任务生命周期中各个阶段变化的事件监听机制,通过这一机制可以在任务的各个阶段做一些自定义的各种动作.SparkListener便是这些 ...
最新文章
- 2021年春季学期-信号与系统-第十二次作业参考答案-第一小题
- python100行代码-python代码统计 100行
- 多个DIV排列时居中
- 桌面快捷键和桌面livefolder
- 虚拟桌面之往事如风 --读书篇
- 接口返回xml格式的数据_接口测试之Jmeter串联xml数据格式请求
- 玩转Light Blue之添加设备信息
- 2018/9/27---高质量SCI论文撰写方法以及ESI引用经验交流 韩光洁教授(大连理工)
- Linux下查看SSD硬盘型号、大小等信息
- 中国微观企业实证研究常用指标集合(2000-2020)
- 介绍一种冷门但简单的双拼方案——紫光双拼
- linux mint 划动鼠标快捷截图
- 耳机串音问题解决(crosstalk)
- Linux高性能服务器编程——书籍阅读笔记
- 17.文件操作(1)----初识文件操作
- PLSQL是什么、为什么要用PLSQL
- unkown the request
- 百度地图获取城市名称
- 查找知乎的真实IP地址
- 快速学会FPGA的首选EDA工具介绍