一步一步教你使用Spring框架中的事件订阅发布
2019/12/31 14:09:58
本文主要是介绍一步一步教你使用Spring框架中的事件订阅发布,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1. 前言
Java 开发中有些逻辑是这样的,完成了A操作,再继续B操作,在继续C操作。这么描述好像有点不清楚。打个比方把,你吃晚饭,通知你老婆(女友)来收碗筷,然后通知你的线上兄弟告诉他们你回来了准备开黑。至于你老婆(女友)来不来收拾无所谓,反正你告诉她了。至于你兄弟你也是通知他们,人家也不一定组你,万一他们正在跟一个一拖三的carry大佬玩的正起劲儿呢。
2. 事件的概念
吃晚饭就是一个所谓的事件。触发了随后的两个操作,他们只存在因果关系。但是它们互不干扰,各自为政。一个完整的事件由 事件源、事件发布、事件监听 组成。 接下来我们聊聊 Spring 中的事件。
3. Spring 中的事件
Spring 框架中使用了大量的事件机制,比如 Spring Boot 的启动。方便起见我们新建一个 Spring Boot 工程。然后跟着我一步步的来进行事件的操作。
3.1 声明事件
- 声明一个事件。通过继承
org.springframework.context.ApplicationEvent
来编写事件。时间里定义好事件推送到监听器需要执行的方法,当然也可以在监听器里写触发逻辑。我们来声明一下:
package cn.felord.boot.event; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; /** * 吃饭事件 * * @author dax * @since 2019 /7/8 21:54 */ @Slf4j public class EatEvent extends ApplicationEvent { private Boolean eatFinished; /** * Instantiates a new Eat event. * * @param eatFinished 吃饭是否完成的信号 这里也可以传递其他资源 */ public EatEvent(Boolean eatFinished) { super(eatFinished); this.eatFinished = eatFinished; } /** * 这里会由对应监听器{@link ApplicationListener<EatEvent>} 执行 * * 叫女友收拾碗筷. */ public void callGirlFriend() { log.info("亲爱的! 我吃完饭了,来收拾收拾吧"); } /** * 这里会由对应监听器{@link ApplicationListener<EatEvent>} 执行 * 呼叫兄弟开黑. */ public void callBrothers() { log.info("兄弟们! 我吃完饭了,带我开黑"); } /** * 吃晚饭的信号. * * @return the boolean */ public Boolean isEatFinished() { return this.eatFinished; } }
3.2 事件发布
发布事件通过实现事件发布接口 org.springframework.context.ApplicationEventPublisher
或者其门面接口 org.springframework.context.ApplicationEventPublisherAware
, 推荐门面接口,里面要定义一个主动推送事件的方法如下面的 refreshEvent
方法,实际代理了 ApplicationEventPublisher
执行其 publishEvent
方法:
package cn.felord.boot.event; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; /** * 发布事件 发布事件通过实现 事件发布接口 {@link ApplicationEventPublisher} * 或者通过门面接口{@link ApplicationEventPublisherAware} * 推荐按照下面的实现方式,而且该类需要注册为spring bean * * @author dax * @since 2019 /7/8 22:04 */ @Slf4j public class EatEventPublisherAware implements ApplicationEventPublisherAware { private ApplicationEventPublisher applicationEventPublisher; private ApplicationEvent eatEvent; public EatEventPublisherAware(ApplicationEvent eatEvent) { this.eatEvent = eatEvent; } @Override public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; } /** * 发送事件动作 事件的动作需要主动触发 调用此方法进行事件触发 * 代理{@link ApplicationEventPublisher#publishEvent(ApplicationEvent)} */ public void refreshEvent() { log.info("发送事件中……"); this.applicationEventPublisher.publishEvent(eatEvent); } }
3.3 事件监听
事件监听用来监听事件以触发相关的逻辑。通过实现 org.springframework.context.ApplicationListener<E extends ApplicationEvent>
来实现事件的监听。特别注意泛型E,如果不指定事件将可以接收任何事件,尽量职责单一。
package cn.felord.boot.event; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationListener; /** * {@link EatEvent}事件的专属事件监听器 * @author dax * @since 2019/7/8 22:11 */ @Slf4j public class EatEventListener implements ApplicationListener<EatEvent> { @Override public void onApplicationEvent(EatEvent eatEvent) { //如果吃完饭了 if (eatEvent.isEatFinished()) { eatEvent.callGirlFriend(); log.error("来自母老虎的咆哮:滚犊子"); eatEvent.callBrothers(); log.error("太晚了,我们已经满了,明天带你"); log.info("还是关注一下 【码农小胖哥】 学习点新知识吧"); } } }
3.4 注入Spring IoC
将上面三个类注入 Spring
容器中,这里我们采用了 JavaConfig 方式,看起来更明显。
package cn.felord.boot.config; import cn.felord.boot.event.EatEvent; import cn.felord.boot.event.EatEventListener; import cn.felord.boot.event.EatEventPublisherAware; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 这三个一定要配置成bean * * @author dax * @since 2019/7/8 22:16 */ @Configuration public class EventConfig { @Bean public ApplicationEvent eatEvent() { return new EatEvent(true); } @Bean public ApplicationListener eatEventListener() { return new EatEventListener(); } @Bean public ApplicationEventPublisherAware eatEventPublisherAware(ApplicationEvent eatEvent) { return new EatEventPublisherAware(eatEvent); } }
4. 测试
这里就大功告成了,那么如何使用呢,执行事件发布器的发布方法 refreshEvent
就行了,事件监听器监听到事件会自动响应。我们来写一个单元测试。
package cn.felord.boot; import cn.felord.boot.event.EatEventPublisherAware; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class) @SpringBootTest public class EventSpringApplicationTests { @Resource private EatEventPublisherAware eatEventPublisherAware; @Test public void contextLoads() { eatEventPublisherAware.refreshEvent(); } }
运行一下,入图
到此你应该就学会使用 Spring 事件了,这样写出来的代码逼格更高。还能提现你对 Spring 框架的一些理解。当然还有一种更加简单的、基于注解的方式,这里不再阐述。相关代码在我的 码云仓库
关注公众号:Felordcn 获取更多资讯
这篇关于一步一步教你使用Spring框架中的事件订阅发布的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?