【Spring】 (6)程序 发布消息 和监听消息
来源:互联网 发布:win10没有网络连接图标 编辑:程序博客网 时间:2024/06/15 18:07
package com.example.demo_2_5;import org.springframework.context.ApplicationEvent;/** * Created by WangBin on 2017/4/17. * 自定义事件 */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.example.demo_2_5;import org.springframework.context.ApplicationListener;import org.springframework.stereotype.Component;/** * Created by WangBin on 2017/4/17. * 事件监听器 */@Component//把普通pojo实例化到spring容器中public class DemoListener implements ApplicationListener<DemoEvent>{//实现ApplicationListenner接口并制定监听的事件 @Override public void onApplicationEvent(DemoEvent demoEvent) {//onApplicationEvent方法 对消息进行接收处理 String msg = demoEvent.getMsg(); System.err.println("接收到消息"+msg); }}
package com.example.demo_2_5;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Component;/** * Created by WangBin on 2017/4/17. * */@Componentpublic class DemoPublisher { @Autowired ApplicationContext applicationContext;//注入ApplicationContext 用来发布事件 【注意是context的包】 public void publish(String msg){ applicationContext.publishEvent(new DemoEvent(this,msg));//调用ApplicationContext的publishEvent方法来发布 }}
package com.example.demo_2_5;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Created by WangBin on 2017/4/17. * */@Configuration@ComponentScan("com.example.demo_2_5")public class EventConfig {}
package com.example.demo_2_5;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created by WangBin on 2017/4/17. * */public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class); DemoPublisher demoPublisher = context.getBean(DemoPublisher.class); demoPublisher.publish("12111111111111111111111111111113"); context.close(); }}
阅读全文
0 0
- 【Spring】 (6)程序 发布消息 和监听消息
- 使用jedis实现Redis消息队列(MQ)的发布(publish)和消息监听(subscribe)
- 深入biztalk消息以及消息订阅发布路由机制(三)-消息发布和路由
- 深入biztalk消息以及消息订阅发布路由机制(三)-消息发布和路由
- ActiveMQ发布消息和订阅消息
- Spring AMQP消息监听容器配置
- Spring之JMS之消息监听容器
- Spring ActiveMQ整合+注解消息监听
- Spring ActiveMQ整合+注解消息监听
- Spring集成rabbitMQ监听消费队列消息
- redis 消息订阅和发布
- 监听剪贴板消息(Delphi)
- 发布消息
- spring---消息订阅发布之redis
- spring---消息订阅发布之RabbiteMQ
- spring---消息订阅发布之JMS
- Spring MVC + redis 消息的订阅发布
- 一个小小的消息发布程序
- Ubuntu16.04+cuda8.0+caffe安装教程
- Groovy脚本热更新
- Find K-th Smallest Pair Distance:查找数组元素中差值第K大的两个元素的差值
- ios 设置状态栏颜色
- interllij IDEA2017快捷键风格修改为myEclipse快捷键风格
- 【Spring】 (6)程序 发布消息 和监听消息
- 能看懂吗?指导一下
- Test
- java类与对象个人理解
- 每天一个linux命令(20):find命令之exec
- java时间格式转换String->long
- ecplise 快捷键大全
- [TI的坑]335x编译环境安装需要更新make至4.1
- JS——我的第一个AJAX程序