第七章 Spring自定义事件
来源:互联网 发布:unity3d开始界面添加 编辑:程序博客网 时间:2024/05/16 02:55
一、自定义事件,集成ApplicationEvent
package demo2.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; }}
二、定义事件监听器,实现ApplicationListener
package demo2.event;import org.springframework.context.ApplicationListener;import org.springframework.stereotype.Component;@Componentpublic class DemoEventListener implements ApplicationListener<DemoEvent> { @Override public void onApplicationEvent(DemoEvent event) { String msg = event.getMsg(); System.out.println("接收到DemoEvent消息:" + msg); }}
三、使用容器发布事件
package demo2;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import demo2.config.DIConfig;import demo2.event.DemoEvent;import demo2.service.DemoService;public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(); cxt.getEnvironment().setActiveProfiles("dev"); cxt.register(DIConfig.class); cxt.refresh(); DemoService ds = cxt.getBean(DemoService.class); //ds.sayDemo("Spring"); cxt.publishEvent(new DemoEvent(cxt, "context init")); //ds.publisher("context init finish"); cxt.close(); }}
运行结果
接收到DemoEvent消息:context init
0 0
- 第七章 Spring自定义事件
- 第七章 事件循环
- spring自定义事件监听
- Spring-自定义事件发布
- Spring IOC 自定义事件
- Spring自定义事件
- Spring中的自定义事件
- Spring学习九:自定义事件
- 如何自定义事件(Spring事件机制浅谈)
- spring事件(三)--自定义事件
- 第七章用户自定义数据类型
- JavaScript Event学习第七章:事件属性
- 第七章 Qt事件(event)处理
- spring中使用自定义的事件
- 第七章 在FLTK中自定义控件
- 第七章 – 自定义Magento系统配置1
- 第七章 – 自定义Magento系统配置2
- 第七章 在FLTK中自定义控件
- oracle 10g 为新建用户并赋予所有表操作权限的方法
- yii框架中 Yii::app()->request
- 购买阿里云服务器CentOs 6.8及搭建lamp环境
- Fragment(碎片机制)的静态加载问题
- swoole学习笔记一
- 第七章 Spring自定义事件
- 曾经所遇到的C/C++与python的技术坑一二
- 6个常见的 PHP 安全性攻击
- matlab 2016b安装libsvm-3.22过程
- [乐意黎转载]AngularJS快速入门指南08:表格
- 多线程中的notify和wait方法的使用
- 2015上机一 输出字符串中连续出现最长的数字串及其开始的下标
- POJ3069(贪心)
- [iOS ] [[NSArray new] makeObjectsPerformSelector:@selector(<#selector#>)];