一、简介

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使用教程(一)的更多相关文章

  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. MySQL分析数据运行状态利器【show full processlist】

    原文地址:https://www.cnblogs.com/shihuc/p/8733460.html 今天的主角是: SHOW [FULL] PROCESSLIST show full process ...

  2. kali渗透综合靶机(十七)--HackInOS靶机

    kali渗透综合靶机(十七)--HackInOS靶机 靶机下载地址:https://www.vulnhub.com/hackinos/HackInOS.ova 一.主机发现 1.netdiscover ...

  3. ASP.NET Core快速入门(第5章:认证与授权)--学习笔记

    课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务31:课时介绍 1.Cookie-based认证与授权 2.Cookie- ...

  4. LinqToSql

    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConsoleA ...

  5. Out,ref,params修饰符,可选参数,命名参数

    out输出,在调用函数中声明,在被调用函数中赋值: ref在调用函数中赋值,后调用: params修饰符,static double CalculateAverage(params[] values) ...

  6. POC挖矿没有前途

    最好的工作量证明是能力证明,能力不适合存储.望文生义,能力的本质意义是“能”和“力”(所有的词汇都按照望文生义理解,因为所有不能望文生义的词汇都是不良的已经被前人修正或将来被后人修正)能力是流动的,迁 ...

  7. easyui高级控件

    开发模式 1. 美工(ui工程师:出一个项目模型) java工程师:将原有的html转成jsp,动态展示数据 缺点: 客户需要调节前端的展示效果 解决:由美工去重新排版,重新选色.2.前后端分离 美工 ...

  8. Linux CentOS 下安装.net core sdk

    注册Microsoft密钥 sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm ...

  9. 一不小心把windows资源管理器给结束任务了 电脑黑屏了 怎么处理

    按键盘上的三个键,Ctrl+Shift+Esc来启动任务管理器: 在任务管理器界面的左上角,有一个“文件”按钮,点击它会出现下拉的“新建任务(运行)...”按钮 鼠标点了“新建任务(运行)...”会弹 ...

  10. requests---自动写博客

    前两天写过一个关于session的博客,session登录过后,可以进行一系列的操作,今天通过模拟登录博客园,自动写博客 自动写博客 我们先理下书写的思路: 1.通过request访问博客园: 2.通 ...