SpringBoot -- 事件(Application Event)
时间:2022-03-12 17:05
Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件。
Spring的事件需要遵循如下流程:
- 自定义事件,集成ApplicationEvent。
- 定义事件监听器,实现ApplicationListener。
- 使用容器发布事件。
一、自定义事件
package com.cenobitor.appevent.event; import org.springframework.context.ApplicationEvent; public class DemoEvent extends ApplicationEvent { private static final long serialVersionUID = 1L; private String msg; public DemoEvent(Object source,String msg) { super(source); this.msg = msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
二、事件监听器
package com.cenobitor.appevent.listener; import com.cenobitor.appevent.event.DemoEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component;
//实现ApplicationListener接口,并指定监听的事件类型 @Component public class DemoListener implements ApplicationListener<DemoEvent> { @Override
//使用onApplicationEvent方法对消息进行接收处理 public void onApplicationEvent(DemoEvent demoEvent) { String msg = demoEvent.getMsg(); System.out.println("(bean-demoListener)接收到了bean-demoPublisher发布的消息:"+msg); } }
三、事件发布类
package com.cenobitor.appevent.publisher; import com.cenobitor.appevent.event.DemoEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @Component public class DemoPublisher { @Autowired
//注入ApplicationContext用来发布事件 ApplicationContext applicationContext; //使用ApplicationContext的publishEvent方法来发布 public void publish(String msg){ applicationContext.publishEvent(new DemoEvent(this,msg)); } }
四、运行
package com.cenobitor.appevent; import com.cenobitor.appevent.publisher.DemoPublisher; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppeventApplication.class); DemoPublisher demoPublisher = context.getBean(DemoPublisher.class); demoPublisher.publish("hello application event"); context.close(); } }
结果:(bean-demoListener)接收到了bean-demoPublisher发布的消息:hello application event
注:摘抄自《JavaEE开发的颠覆者SpringBoot 实战》。
相关推荐
- Android系统编程入门系列之界面Activity交互响应
- 新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- uni-app滚动视图容器(scroll-view)之监听上拉事件
- uniapp h5,app两端复制文本
- Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
- 诺基亚短信生成!太好玩了
- iOS 跳转App Store进行评分
- 开发一个即时通讯App
- 关闭苹果IOS app自动更新
电脑软件
本类排行
- 1关闭苹果IOS app自动更新
- 2iOS 跳转App Store进行评分
- 3诺基亚短信生成!太好玩了
- 4Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 5uniapp h5,app两端复制文本
- 6uni-app滚动视图容器(scroll-view)之监听上拉事件
- 7新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- 8Android系统编程入门系列之界面Activity交互响应
- 9开发一个即时通讯App
- 10【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn