spring中怎么监听ApplicationEvent事件现
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章将为大家详细讲解有关spring中怎么监听ApplicationEvent事件现,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。原理:Appli
千家信息网最后更新 2026年11月02日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安全错误
数据库的锁怎样保障安全
咖啡豆
广州紫鲸互联网科技有限公司
科技赋能下的互联网金融
中国 科技 互联网
西安互联网科技
互联网 卓越科技
善班互联网科技有限公司
时间互联网络科技
四川狮子座互联网科技
中国科技互联网公司收入
中国互联网有什么科技含量
消费医药和互联网科技
国大互联网教育科技有限公司工作
乌鲁木齐新华智原互联网科技学校
杭州披云加互联网科技有限公司
美国互联网科技公司
互联网科技股为什么大涨
互联网硬科技公司
互联网科技创新创业项目
杭州科技互联网公司有哪些
咖啡网
互联网科技
万易互联网科技有限公司
互联网科技公司做什么
美国互联网前沿科技
江苏南京365互联网科技
北京市互联网新科技
互联网风格科技公司名称
上海万顺科技互联网有限公司招聘
中国互联网公司的科技投入