spring中怎么监听ApplicationEvent事件现
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,这篇文章将为大家详细讲解有关spring中怎么监听ApplicationEvent事件现,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。原理:Appli
千家信息网最后更新 2025年12月03日spring中怎么监听ApplicationEvent事件现
这篇文章将为大家详细讲解有关spring中怎么监听ApplicationEvent事件现,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
原理:ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播。通过ApplicationContextAware我们可以把系统中所有ApplicationEvent传播给系统中所有的ApplicationListener。
1、直接上代码
2、定义自己的监听器(负责处理自己的监听事件)
3、定义一个bean触发监听事件
4、测试
package com.test.eventListener;import org.springframework.context.ApplicationEvent;/** * [@author](https://my.oschina.net/arthor) admin * [@date](https://my.oschina.net/u/2504391) 2018/5/17 17:37 * 新建StudentAddEvent类,实现抽象类org.springframework.context.ApplicationEvent * StudentAddEvent类中需要实现自己的构造函数 * 增加学生监听事件 */public class StudentAddEvent extends ApplicationEvent { private static final long serialVersionUID = 20L; /** * 学生姓名 */ private String name; /** * [@param](https://my.oschina.net/u/2303379) source */ public StudentAddEvent(Object source, String name) { super(source); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}package com.test.eventListener;import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationListener;import org.springframework.stereotype.Component;/** * [@author](https://my.oschina.net/arthor) admin * 新建StudentAddListener类,实现接口org.springframework.context.ApplicationListener中的onApplicationEvent方法, * 在该方法中只处理StudentAddEvent类型的ApplicationEvent事件 * 定义StudentAddListener监听器 */[@Component](https://my.oschina.net/u/3907912)public class StudentAddListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { // 1.判断是否是增加学生对象的事件 if (!(event instanceof StudentAddEvent)) { return; } // 2.是增加学生事件的对象,进行逻辑处理,比如记日志、积分等 StudentAddEvent studentAddEvent = (StudentAddEvent) event; System.out.println("增加了学生:" + studentAddEvent.getName()); }}package com.test.eventListener;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;/** * @author admin * 定义StudentAddBean触发StudentAddEvent事件 * 新建StudentAddBean类,实现接口 org.springframework.context.ApplicationContextAware中的setApplicationContext方法, * 在构造bean的时候注入Spring的上下文对象,以便通过Spring上下文对象的publishEvent方法来触发StudentAddEvent事件 */@Componentpublic class StudentAddBean implements ApplicationContextAware { /** * 定义Spring上下文对象 */ private ApplicationContext applicationContext = null; /* * (non-Javadoc) * * @see * org.springframework.context.ApplicationContextAware#setApplicationContext * (org.springframework.context.ApplicationContext) */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * 增加一个学生 * * @param studentName */ public void addStudent(String studentName) { // 1.构造一个增加学生的事件 StudentAddEvent aStudentEvent = new StudentAddEvent( applicationContext, studentName); // 2.触发增加学生事件 applicationContext.publishEvent(aStudentEvent); }}package com.test.eventListener;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author admin * ApplicationContext在运行期会自动检测到所有实现了ApplicationListener的bean对象,并将其作为事件接收对象。 * 当ApplicationContext的publishEvent方法被触发时,每个实现了ApplicationListener接口的bean都会收到ApplicationEvent对象, * 每个ApplicationListener可根据事件类型只接收处理自己感兴趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。 */public class EventListenerTest { public static void main(String[] args) { String[] xmlConfig = new String[] { "spring/spring.xml" }; // 使用ApplicationContext来初始化系统 ApplicationContext context = new ClassPathXmlApplicationContext(xmlConfig); StudentAddBean studentBean = (StudentAddBean) context.getBean("studentAddBean"); studentBean.addStudent("张三"); studentBean.addStudent("李四"); }}关于spring中怎么监听ApplicationEvent事件现就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
事件
学生
对象
监听
方法
接口
处理
上下
上下文
系统
传播
内容
文章
更多
监听器
知识
篇文章
类型
不错
代码
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
HP 服务器光口连磁盘柜
肇庆通讯软件开发零售价
常见的电子图书下载数据库
坪山区标准网络技术
没pb服务器
软件开发开票免税
植物常用数据库
数据库2705错误
服务器二手主板家用
数据库建好表格无法保存
火车头采集写入数据库失败
ios12 软件开发
位置服务器怎么装
河北网络安全知识答题秦皇岛
枣庄网络安全招聘
hadoop数据库备份恢复
笔记本更新后显示服务器登录失败
新泰oa办公软件开发联系方式
软件开发科技型企业估值
关于网络安全知识的视频培训
厦门政府征信数据库
实用网络技术中名词解释ftp
pop接收服务器
监控服务器系统设计
mctnt服务器
网络安全教育晨会新闻稿
网络赌博棋牌软件开发
供销社网络安全工作计划
网络安全红蓝队攻防演练新闻
国产数据库差距