Java Spring 自定义事件监听
ApplicationContext 事件
定义一个context的起动监听事件
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent; public class EventStart implements ApplicationListener<ContextStartedEvent>{ @Override
public void onApplicationEvent(ContextStartedEvent arg0) {
System.out.println("上下文 开始 事件");
}
}
要定义一个事件监听,首先你得有一个事件,ContextStartedEvent 是一个固定的、具体的事件,Java spring自带的,通过实现
ApplicationListener<ContextStartedEvent >
就可以监控这个事件了
ContextStartedEvent 的定义
@SuppressWarnings("serial")
public class ContextStartedEvent extends ApplicationContextEvent {
/**
* Create a new ContextStartedEvent.
* @param source the {@code ApplicationContext} that has been started
* (must not be {@code null})
*/
public ContextStartedEvent(ApplicationContext source) {
super(source);
}
}
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent; public class EventStop implements ApplicationListener<ContextStoppedEvent> { @Override
public void onApplicationEvent(ContextStoppedEvent arg0) {
System.out.println("上下文 停止 事件");
}
}
主方法
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("b.xml");
context.start();
Dept dept = (Dept)context.getBean("dept");
dept.getLeader();
context.stop();
context.registerShutdownHook();
b.xml
<bean id="eventStart" class="test4.EventStart"></bean>
<bean id="eventStop" class="test4.EventStop"></bean>
输出
初始化 bean.洪七公
初始化 bean.null
家
我
上下文 开始 事件
部门领导洪七公
上下文 停止 事件
销毁 bean.null
销毁 bean.洪七公
为什么主方法中一调用 start 方法,监听事件就可以自己被调用 呢
start方法真正执行的是AbstractApplicationContext的start方法,在方法里,除了真正start要处理的内容外,还额外加了事件处理,
@Override
public void start() {
getLifecycleProcessor().start();
publishEvent(new ContextStartedEvent(this));
}
所加的事件就是ContextStartedEvent事件,在将实例了ApplicationListener<ContextStartedEvent>的bean通过xml注册到spring容器中时,容器自动调用了该bean的onApplicationEvent方法
自定义事件
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Work extends ApplicationEvent{
private static final long serialVersionUID = 1L;
private Person ps;
public void setPs(Person ps) {
this.ps = ps;
}
public Work(Object source) {
super(source);
}
public void doWorking() {
String name = this.ps.getName();
System.out.println(name + " 今天没有完成100张图片制作,并且听到这个任务时,情绪时而激动,时而低落");
}
}
//该类要注册到spring容器,然后容器会自动调用实现ApplicationListener接口的类的onApplicationEvent方法
public class WorkListener implements ApplicationListener<Work>{ @Override
public void onApplicationEvent(Work event) {
// TODO Auto-generated method stub
System.out.println("摄像头记录到:");
event.doWorking();
}
}
public class Sleep extends ApplicationEvent{
private Person ps;
public void setPs(Person ps) {
this.ps = ps;
}
private static final long serialVersionUID = 2L;
public Sleep(Object source) {
super(source);
}
public void doSleeping() {
String name = this.ps.getName();
System.out.println(name + " 开始休息了");
}
}
//该类要注册到spring容器,然后容器会自动调用实现ApplicationListener接口的类的onApplicationEvent方法
public class SleepListener implements ApplicationListener<Sleep> { @Override
public void onApplicationEvent(Sleep event) {
System.out.println("摄像头记录到:");
event.doSleeping();
}
}
public class Report implements ApplicationEventPublisherAware{
private ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
public void work() {
System.out.println("开始小明的工作报告");
Work work = new Work(this);
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("d.xml");
Person xm = (Person)context.getBean("xiaoming");
work.setPs(xm);
this.publisher.publishEvent(work);
}
public void sleep() {
System.out.println("开始小明的睡眠报告");
Sleep sl = new Sleep(this);
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("d.xml");
Person xm = (Person)context.getBean("xiaoming");
sl.setPs(xm);
this.publisher.publishEvent(sl);
}
}
<bean name="xiaoming" class="test6.Person">
<property name="name" value="小明"></property>
</bean> <bean id="wkReport" class="test6.Report"></bean>
<bean id="wkMonitor" class="test6.WorkListener"></bean>
<bean id="slMonitor" class="test6.SleepListener"></bean>
public class Main {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("d.xml");
Report rp = (Report)context.getBean("wkReport");
rp.work();
rp.sleep();
context.close();
}
}
输出
开始小明的工作报告
摄像头记录到:
小明 今天没有完成100张图片制作,并且听到这个任务时,情绪时而激动,时而低落
开始小明的睡眠报告
摄像头记录到:
小明 开始休息了
事件本身,Work,Sleep不需要注册到sping容器中。
想要让
rp.work();rp.sleep();
工作,睡觉这样的方法/行为,可以被监控,除了这些方法本身要处理的内容外,
还需要为这个方法定义一个具体的事件类(此例中是Work,Sleep,这些具体的事件类不需要注册到spring容器,因为他们在工作、睡觉这些方法中已经被调用了),
然后将对应的事件类添加到ApplicationEventPublisher事件处理的逻辑中;
在我们调用 工作、睡觉这些方法时,就会触发事件处理逻辑,
spring容器会自动检测哪些bean实现了与之相对应的ApplicationListener监听类,然后调用其onApplicationEvent方法
Java Spring 自定义事件监听的更多相关文章
- Spring之事件监听(观察者模型)
目录 Spring事件监听 一.事件监听案例 1.事件类 2.事件监听类 3.事件发布者 4.配置文件中注册 5.测试 二.Spring中事件监听分析 1. Spring中事件监听的结构 2. 核心角 ...
- Spring的事件监听机制
最近公司在重构广告系统,其中核心的打包功能由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统.因此,为了将apk打包的核心流程和对接广告系统的业务解耦,利用了spr ...
- [问题贴]mui.openWindow+自定义事件监听操作让alert()执行两次
仔细看,Alert函数执行了两次 共两个页面:index.html和detail.html, detail.html为按钮设置了自定义事件监听(newsId),触发alert. 在index.html ...
- 十一、Spring之事件监听
Spring之事件监听 ApplicationListener ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成Applica ...
- Spring的事件监听ApplicationListener
ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Applica ...
- Java中的事件监听机制
鼠标事件监听机制的三个方面: 1.事件源对象: 事件源对象就是能够产生动作的对象.在Java语言中所有的容器组件和元素组件都是事件监听中的事件源对象.Java中根据事件的动作来区分不同的事件源对象,动 ...
- java Gui编程 事件监听机制
1. GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在DOS命令行中通过javac java命令启动程序. 软件的交互的方式: 1. 命令交互方式 图书管理系统 ...
- Java界面编程—事件监听机制
组件首先要先注册事件处理器,当用户单击组件.移动鼠标或者敲击键盘时都会产生事件(Event),一旦有时间发生,应用程序就会做出对该事件的响应,这些组件就是事件源(Event source). 接受.解 ...
- Java 中的事件监听机制
看项目代码时遇到了好多事件监听机制相关的代码.现学习一下: java事件机制包含三个部分:事件.事件监听器.事件源. 1.事件:继承自java.util.EventObject类,开发人员自己定义. ...
随机推荐
- ansible简介安装配置
ansible简介 ansible是一款,自动化运维管理工具.顾名思义是用于批量去管理及安装服务及批量管理主机. ansible与saltstack对比 ansible优点:配置简单,部署容易除主管理 ...
- VSCode Java 开发环境配置 详细教程
VSCode Java 开发环境配置 详细教程 配置java 下载 用于现在大多数使用者用的是java 8,小白的我先安装java 8好了,^ w ^. 下载地址:Java 8 | Java SE 打 ...
- 攻防世界 WEB 高手进阶区 XCTF Web_python_template_injection Writeup
攻防世界 WEB 高手进阶区 XCTF Web_python_template_injection Writeup 题目介绍 题目考点 SSTI模板注入漏洞 Writeup 知识补充 模板注入:模板引 ...
- 大一C语言学习笔记(11)---编程篇--写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积,要求 0 bug;
考核内容: 写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积: 答案: #include<stdio.h ...
- [loj2469]最小方差生成树
2018年论文题 约定:令点集$V=[1,n]$.边集$E=[1,m]$,记$m$条边依次为$e_{i}=(x_{i},y_{i},c_{i})$(其中$1\le i\le m$),将其按照$c_{i ...
- [hdu6598]Harmonious Army
网络流建图,首先将所有价值加起来,用最小割考虑要删掉多少个价值:源点向每一个士兵连流量为x的边,士兵向汇点连流量为y的边,每一对关系间连流量为z的边,考虑有方程x1+y2+z=x2+y1+z=a+c, ...
- [bzoj5025]单调上升路径
由于题目的证明可以发现$ans\ge 2m/n \ge n-1$,于是大胆猜测答案就是n-1若n是奇数,则将边分为n组,每组(n-1)/2,如果同组内边没有交点,那么只需要每一组边一个权值区间,从每一 ...
- 互联网java面试宝典
1.为什么使用消息队列啊? 答题: 消息队列的核心功能就是:解耦合,异步,流量削峰解耦:接口调用发送,那如果E系统也要这个数据呢?那如果C系统现在不需要了呢?现在A系统又要发送第二种数据了呢?A系统负 ...
- 难道你还不知道Spring之事务的回滚和提交的原理吗,这篇文章带你走进源码级别的解读。
上一篇文章讲解了获取事务,并通过获取的connection设置只读,隔离级别等:这篇文章讲事务剩下的回滚和提交. 事务的回滚处理 之前已经完成了目标方法运行前的事务准备工作.而这些准备工作的最大目的无 ...
- 【NOI导刊200908模拟试题02 题4】【二分+Dijkstra】 收费站
Description 在某个遥远的国家里,有n个城市.编号外1,2,3,-,n. 这个国家的政府修建了m条双向的通路.每条公路连接着两个城市.沿着某条公路,开车从一个城市到另一个城市,需要花费一定的 ...