为JFinal添加event消息事件

本文探讨了Spring事件驱动模型在复杂业务场景中的应用,并对比了Guava EventBus的特点。通过具体的案例分析,阐述了如何选择合适的事件发布订阅模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在之前使用spring mvc的时候,在复杂的下单和支付中有一部分功能使用的Spring事件驱动模型去完成!具体优点不啰嗦了,发现涛哥有篇文章讲得比较详细:详解Spring事件驱动模型

最初准备基于Guava 的Eventbus去实现,发现其并不太适合!以下来自官方的wiki

Why must I create my own Event Bus, rather than using a singleton? EventBus doesn't specify how you use it; there's nothing stopping your application from having separate EventBus instances for each component, or using separate instances to separate events by context or topic. This also makes it trivial to set up and tear down EventBus objects in your tests.

Of course, if you'd like to have a process-wide EventBus singleton, there's nothing stopping you from doing it that way. Simply have your container (such as Guice) create the EventBus as a singleton at global scope (or stash it in a static field, if you're into that sort of thing).

In short, EventBus is not a singleton because we'd rather not make that decision for you. Use it how you like.

JFinal-event实现得比较简单,整个代码结构如图: 代码结构

其核心原理是,使用guava Multimap转载所有的监听器和监听器监听的泛型ApplicationEvent的子类!

源码地址:

http://git.oschina.net/596392912/JFinal-event

欢迎拍砖~.~

参考代码:

https://github.com/google/guava/tree/master/guava/src/com/google/common/eventbus

https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/java/org/springframework/context/event

转载于:https://my.oschina.net/qq596392912/blog/407410

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值