一、简介

EventBus是一个基于发布订阅的事件总线,在Java和Android里都可以使用。

二、使用

1.引入pom

  1. <dependency>
  2. <groupId>org.greenrobot</groupId>
  3. <artifactId>eventbus</artifactId>
  4. <version>3.1.1</version>
  5. </dependency>

2.定义事件监听类

  1. @Component
  2. @Slf4j
  3. public class EventListener {
  4. @Subscribe
  5. public void onMessageEvent(String event) {
  6. log.info("Subscribe message:{}", event);
  7. }
  8. }

3.EventBusConfig

  1. @Configuration
  2. public class EventBusConfig {
  3.  
  4. @Bean
  5. public EventBus eventBus(){
  6. return new EventBus();
  7. }
  8.  
  9. }

4.EventHandler

  1. @Component
  2. @Slf4j
  3. public class EventHandler {
  4.  
  5. @Autowired
  6. private EventBus eventBus;
  7.  
  8. @Autowired
  9. private EventListener eventListener;
  10.  
  11. @PostConstruct
  12. public void init() {
  13. eventBus.register(eventListener);
  14. }
  15.  
  16. @PreDestroy
  17. public void destroy() {
  18. eventBus.unregister(eventListener);
  19. }
  20.  
  21. public void eventPost(){
  22. eventBus.post("test");
  23. log.info("post event");
  24. }
  25. }

5.测试类

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class EventBusTest {
  4.  
  5. @Autowired
  6. private EventHandler eventHandler;
  7.  
  8. @Test
  9. public void postEvent(){
  10. eventHandler.eventPost();
  11. }
  12.  
  13. }

6.运行结果

  1. 2019-11-01 22:33:24.657 INFO 6656 --- [ main] com.sww.bizhi.eventbus.EventListener : Subscribe message:test
  2. 2019-11-01 22:33:24.671 INFO 6656 --- [ main] com.sww.bizhi.eventbus.EventHandler : post event

三、自定义事件

上面的例子,我们发送和接收的都是string,如果我想event是个bean,怎么操作?

1.添加MessageEvent类

  1. @Data
  2. @Builder
  3. public class MessageEvent {
  4.  
  5. private Integer id;
  6. private String name;
  7.  
  8. }

2.修改EventHandler里的eventPost方法(红色字体部分)

  1. public void eventPost(){
  2. eventBus.post(MessageEvent.builder().id(1).name("test").build());
  3. log.info("post event");
  4. }

3.修改EventListener类里的onMessageEvent方法(红色字体部分)

  1. @Subscribe
  2. public void onMessageEvent(MessageEvent event) {
  3. log.info("Subscribe message:{}", event);
  4. }

4.运行测试类,查看结果

  1. 2019-11-01 22:53:24.273 INFO 2252 --- [ main] com.sww.bizhi.eventbus.EventListener : Subscribe message:MessageEvent(id=1, name=test)
  2. 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使用教程(一)的更多相关文章

  1. SpringBoot+EventBus使用教程(二)

    简介 继续上篇,本篇文章介绍如何集成spring-boot-starter-guava-eventbus使用EventBus,最新的版本好像已经不叫spring-boot-starter-guava- ...

  2. 初识springboot(傻瓜式教程)

    初识springboot(傻瓜式教程) 项目所需的版本 IDEA 2018 maven 3.x jdk-1.8 IDEA创建spring-boot项目(maven方法) 1.创建一个maven工程 点 ...

  3. 很详细的SpringBoot整合UEditor教程

    很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529    版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...

  4. springboot+shiro整合教程

    进阶教程: 1. springboot+shiro+redis(单机redis版)整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+s ...

  5. 千锋很火的SpringBoot实战开发教程视频

    springboot是什么? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...

  6. SpringBoot之入门教程-SpringBoot项目搭建

    SpringBoot大大的简化了Spring的配置,把Spring从配置炼狱中解救出来了,以前天天配置Spring和Mybatis,Springmvc,Hibernate等整合在一起,感觉用起来还是挺 ...

  7. SpringBoot整合Guacamole教程

    前言 本文主要介绍的是SpringBoot如何整合Guacamole在浏览器是远程桌面的访问. Guacamole 介绍 Apache Guacamole 是一个无客户端远程桌面网关.它支持标准协议, ...

  8. 《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 ...

  9. EventBus使用教程

    如图准备工作: 父子(子父)组件触发 EventBus.$emit('sub') EventBus.$on('sub',()=>{ console.log(1111222232211122) } ...

随机推荐

  1. JavaScript的__proto__、prototype和继承

    JavaScript也是可以“继承”的! 各位看官或是好奇,或是一知半解.什么是prototype,__proto__,constructor.哪种继承方式好.今天就在这交流交流. 什么是protot ...

  2. 2019.10 搜索引擎最新排名,Elasticsearch遥遥领先

    大数据的搜索平台已经成为了众多企业的标配,Elasticsearch.Splunk(商业上市公司).Solr(Apache开源项目)是其中最为优秀和流行的选择.在2019.10 最新搜索引擎排名中,E ...

  3. OpenGL入门1.1:窗口

    每一个小步骤的源码都放在了Github 的内容为插入注释,可以先跳过 测试GLFW 在我们的test.cpp中加入下面两个头文件 #include <glad/glad.h> #inclu ...

  4. 2019-11-26-C#-判断方法是否被子类重写

    原文:2019-11-26-C#-判断方法是否被子类重写 title author date CreateTime categories C# 判断方法是否被子类重写 lindexi 2019-11- ...

  5. 【设计模式】Singleton

    前言 Singleton设计模式,确保全局只存在一个该类的实例.将构造器声明为private,防止调用(虽然还是可以使用反射来调用).声明一个静态的类实例在类中,声明一个公共的获取实例的方法.这篇博文 ...

  6. npm和cnpm命令后无响应

    问题: 1.把前端环境配制完毕之后,打开项目,输入cnpm install之后,光标一直在另起一行的位置闪,但是丝毫没有在安装的迹象. 2.打开cmd,在窗体中输入node -v 可以显示版本,但是输 ...

  7. Windows中将nginx添加到服务(转)

    下载安装nginx http://nginx.org/en/download.html 下载后解压到C盘 C:\nginx-1.14.0 添加服务 需要借助"Windows Service ...

  8. 解决 bash: vue command not found

    背景  : win10 使用  yarn  全局 安装  vue/cli 后   yarn  global add  @vue/cli 提示安装成功 使用vue create   提示  bash: ...

  9. E203 同步fifo

    1. 输入端, 输入信号, i_vld,表示输入请求写同步fifo,如果fifo不满,则fifo发送i_rdy 到输入端,开始写fifo.i_vld和i_rdy是写握手信号. 2.输出端 o_rdy表 ...

  10. Qt导航栏 QListWidget

    使用Qt Designer 使用QListWidget控件 设置样式 QListWidget::item { min-height: 30px; /*设置item高度*/ border-style: ...