BlogApp之spring整合Guava中的AsyEventBus异步事件
2016-02-13 10:01
513 查看
在这个博客的创作中,有许多地方需要使用到异步事件,在此选择了google的guava,这个里面有许多好用的工具库,
工程所需的maven依赖为:
这里spring和AsyncEventBus的整合相对的整合要麻烦一些,EventBus的构造函数中有一个无参的构造函数,可以直接注入,但是
AsyncEventBus没有无参构造函数,最少也要有一个Executor的参数,因此在此主要编写这个参数的注入。
AsyncExecutor的编写为:
如此基本就完成了Executor的编写,现在需要一个工厂类ExecutorFactory。
ExecutorFactory:
现在基本完成工厂类的编写,现在来编写配置文件:
如此,基本AsyncEventBus的配置就完成了,
但在handler的编写中,仍要将handler写进spring的配置文件中,以便方便注入。
在这里,如果有扫描的方法,还请其他人指教,毕竟在这里,有相当多的handler需要编写,每个handler都配置是比较麻烦的。
具体AsyncEventBus和spring的整合大致就到这里了。
工程所需的maven依赖为:
<!--guava--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency>
这里spring和AsyncEventBus的整合相对的整合要麻烦一些,EventBus的构造函数中有一个无参的构造函数,可以直接注入,但是
AsyncEventBus没有无参构造函数,最少也要有一个Executor的参数,因此在此主要编写这个参数的注入。
AsyncExecutor的编写为:
@Data public class AsyncExecutor { private Executor executor; public AsyncExecutor() { executor = Executors.newFixedThreadPool(10); } }
如此基本就完成了Executor的编写,现在需要一个工厂类ExecutorFactory。
ExecutorFactory:
public class ExecutorFactory { public Executor getExecutorService(){ return Executors.newFixedThreadPool(8); } }
现在基本完成工厂类的编写,现在来编写配置文件:
<bean id="executorFactory" class="cn.com.factory.ExecutorFactory" /> <bean id="executor" factory-bean="executorFactory" factory-method="getExecutorService"></bean> <bean id="eventBus" class="com.google.common.eventbus.AsyncEventBus"> <constructor-arg ref="executor" /> </bean>
如此,基本AsyncEventBus的配置就完成了,
但在handler的编写中,仍要将handler写进spring的配置文件中,以便方便注入。
在这里,如果有扫描的方法,还请其他人指教,毕竟在这里,有相当多的handler需要编写,每个handler都配置是比较麻烦的。
具体AsyncEventBus和spring的整合大致就到这里了。
相关文章推荐
- 如何在Android应用程序中保存数据
- Android开发学习之路-回调实现Service向activity传递数据
- Android中Handler的进阶使用方式
- android 无线调试
- 如何在Android智能手机上捕获数据包
- 关于drawRect:方法apple官方文档的翻译
- 深入分析LayoutInflater
- IOS不用AutoLayout也能实现自动布局的类(5)----MyFlowLayout横空出世
- 在ios中使用soundtouch库实现变声
- Android学习-多媒体应用
- [基础知识] 总结使用Unity 3D优化游戏运行性能的经验
- 微信朋友圈闪退代码是什么?微信朋友圈闪退代码使用方法
- Unity打造简易的GalGame游戏剧本编辑引擎
- Hikey Android 6.0 版本构建
- 移动端第三方登录(微信)java验证并获取用户信息
- ios 泛型
- IOS开发--待研究源码(持续添加更新)
- Android Touch事件传递机制解析 (推荐)
- Android Wear 进阶 - 1 Notification
- demo 微信毛玻璃效果