SpringBoot+EventBus使用教程(一)
一、简介
EventBus是一个基于发布订阅的事件总线,在Java和Android里都可以使用。

二、使用
1.引入pom
<dependency>
<groupId>org.greenrobot</groupId>
<artifactId>eventbus</artifactId>
<version>3.1.1</version>
</dependency>
2.定义事件监听类
@Component
@Slf4j
public class EventListener {
@Subscribe
public void onMessageEvent(String event) {
log.info("Subscribe message:{}", event);
}
}
3.EventBusConfig
@Configuration
public class EventBusConfig { @Bean
public EventBus eventBus(){
return new EventBus();
} }
4.EventHandler
@Component
@Slf4j
public class EventHandler { @Autowired
private EventBus eventBus; @Autowired
private EventListener eventListener; @PostConstruct
public void init() {
eventBus.register(eventListener);
} @PreDestroy
public void destroy() {
eventBus.unregister(eventListener);
} public void eventPost(){
eventBus.post("test");
log.info("post event");
}
}
5.测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class EventBusTest { @Autowired
private EventHandler eventHandler; @Test
public void postEvent(){
eventHandler.eventPost();
} }
6.运行结果
2019-11-01 22:33:24.657 INFO 6656 --- [ main] com.sww.bizhi.eventbus.EventListener : Subscribe message:test
2019-11-01 22:33:24.671 INFO 6656 --- [ main] com.sww.bizhi.eventbus.EventHandler : post event
三、自定义事件
上面的例子,我们发送和接收的都是string,如果我想event是个bean,怎么操作?
1.添加MessageEvent类
@Data
@Builder
public class MessageEvent { private Integer id;
private String name; }
2.修改EventHandler里的eventPost方法(红色字体部分)
public void eventPost(){
eventBus.post(MessageEvent.builder().id(1).name("test").build());
log.info("post event");
}
3.修改EventListener类里的onMessageEvent方法(红色字体部分)
@Subscribe
public void onMessageEvent(MessageEvent event) {
log.info("Subscribe message:{}", event);
}
4.运行测试类,查看结果
2019-11-01 22:53:24.273 INFO 2252 --- [ main] com.sww.bizhi.eventbus.EventListener : Subscribe message:MessageEvent(id=1, name=test)
2019-11-01 22:53:24.278 INFO 2252 --- [ main] com.sww.bizhi.eventbus.EventHandler : post event
四、最后
本篇初步介绍了如何在springboot里使用eventbus,下一篇文章将介绍如何集成spring-boot-starter-guava-eventbus
EventBus地址:https://github.com/greenrobot/EventBus
另外,本篇教程使用了lombok插件。
本文系本人原创,同步更新在我的独立博客http://791202.com/上,如要转载,请注明出处!
SpringBoot+EventBus使用教程(一)的更多相关文章
- SpringBoot+EventBus使用教程(二)
简介 继续上篇,本篇文章介绍如何集成spring-boot-starter-guava-eventbus使用EventBus,最新的版本好像已经不叫spring-boot-starter-guava- ...
- 初识springboot(傻瓜式教程)
初识springboot(傻瓜式教程) 项目所需的版本 IDEA 2018 maven 3.x jdk-1.8 IDEA创建spring-boot项目(maven方法) 1.创建一个maven工程 点 ...
- 很详细的SpringBoot整合UEditor教程
很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529 版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...
- springboot+shiro整合教程
进阶教程: 1. springboot+shiro+redis(单机redis版)整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+s ...
- 千锋很火的SpringBoot实战开发教程视频
springboot是什么? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- SpringBoot之入门教程-SpringBoot项目搭建
SpringBoot大大的简化了Spring的配置,把Spring从配置炼狱中解救出来了,以前天天配置Spring和Mybatis,Springmvc,Hibernate等整合在一起,感觉用起来还是挺 ...
- SpringBoot整合Guacamole教程
前言 本文主要介绍的是SpringBoot如何整合Guacamole在浏览器是远程桌面的访问. Guacamole 介绍 Apache Guacamole 是一个无客户端远程桌面网关.它支持标准协议, ...
- 《Springboot极简教程》问题解决:Springboot启动报错 Whitelabel Error Page: This application has no explicit mapping for(转)
13.2 Spring Boot启动报错:Whitelabel Error Page 13.2 Spring Boot启动报错:Whitelabel Error Page 问题描述 Whitelabe ...
- EventBus使用教程
如图准备工作: 父子(子父)组件触发 EventBus.$emit('sub') EventBus.$on('sub',()=>{ console.log(1111222232211122) } ...
随机推荐
- Elasticsearch(ES) 创建索引
欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...
- Elasticsearch 使用 php curl 插入数据
<?php /** * Created by PhpStorm. * User: func7 * Date: 2018/11/8 * Time: 11:24 */ set_time_limit( ...
- QT+OpenGL(03)--libpng库的编译
1.zlib库的下载 http://www.zlib.net/ zlib1211.zip 2.libpng库的下载 https://libpng.sourceforge.io/index.html l ...
- 大文件SQl脚本怎么还原以及SQlsqlserver怎么全自动备份数据库
1:导出的SQl脚本文件通常大于100M左右就会还原不了,不是报错就是说系统文件找不到(sql脚本是直接拖进来的,不存在路径的问题). 2:CMD 使用 OSQL命令或者Sqlcmd命令都是可以解决的 ...
- python 排序 拓扑排序
在计算机科学领域中,有向图的拓扑排序是其顶点的先行排序,对于每个从顶点u到顶点v的有向边uv,在排序的结果中u都在v之前. 如果图是有向无环图,则拓扑排序是可能的(为什么不说一定呢?) 任何DAG具有 ...
- 关于PHP在企业级开发领域的访谈
企业软件的一个关键元素就是互操作性,它可以让软件与其他平台交换信息.大家都认为PHP在这方面表现欠佳,因为它的WS-*支持相对来说比较新且功能较少,成熟度不高.关于这点我们从未手动开启过PHP的相关进 ...
- HTTPS与HTTP区别
HTTP + 加密 + 认证 + 完整性保护 = HTTPS http的全称是Hypertext Transfer Protocol Vertion (超文本传输协议) HTTPS: HTTPS(Se ...
- Android 主题、样式
样式是针对View的,比如TextView.Button等控件,主题是针对Activity.整个APP的. 样式.主题是多种属性的集合,类似于网页中的CSS样式,可以让设计与内容分离,并且可以继承.复 ...
- nrm : 无法加载文件 C:\Users\TANG\AppData\Roaming\npm\nrm.ps1,因为在此系统上禁止运行脚本。
1.win+s 搜索powershell 以管理身份运行 2.使用set-ExecutionPolicy RemoteSigned命令将计算机上的执行策略更改为 RemoteSigned,输入Y确定 ...
- 很不错的python 机器学习资源
http://www.cuijiahua.com/resource.html 曾看过的书,感觉一些很有用的学习资料,推荐给大家! Python基础: 网络教程推荐: 系统学习python3可以看廖雪峰 ...