JSF基于事件的交流:新派方法
番石榴EventBus
Google Guava库具有有用的package eventbus 。 EventBus类允许组件之间进行发布-订阅式通信,而无需组件之间进行显式注册。 因为我们开发Web应用程序,所以我们应该将此类的实例封装在有作用域的bean中。
让我们编写EventBusProvider bean。
public class EventBusProvider implements Serializable {private EventBus eventBus = new EventBus("scopedEventBus");public static EventBus getEventBus() {// access EventBusProvider beanELContext elContext = FacesContext.getCurrentInstance().getELContext();EventBusProvider eventBusProvider =(EventBusProvider) elContext.getELResolver().getValue(elContext, null, "eventBusProvider");return eventBusProvider.eventBus;}
}
我仅以一个示例来演示Guava EventBus的所有主要功能。 让我们编写以下事件层次结构:
public class SettingsChangeEvent {}public class LocaleChangeEvent extends SettingsChangeEvent {public LocaleChangeEvent(Object newLocale) {...}
}public class TimeZoneChangeEvent extends SettingsChangeEvent {public TimeZoneChangeEvent(Object newTimeZone) {...}
}
public MyBean1 implements Serializable {@PostConstructpublic void initialize() throws Exception {EventBusProvider.getEventBus().register(this);}@Subscribepublic void handleLocaleChange(LocaleChangeEvent event) {// do something}@Subscribepublic void handleTimeZoneChange(TimeZoneChangeEvent event) {// do something}
}public MyBean2 implements Serializable {@PostConstructpublic void initialize() throws Exception {EventBusProvider.getEventBus().register(this);}@Subscribepublic void handleSettingsChange(SettingsChangeEvent event) {// do something}
}
要发布事件,只需将事件对象提供给EventBus实例的post()方法。 EventBus实例将确定事件的类型并将其路由到所有已注册的侦听器。
public class UserSettingsForm implements Serializable {private boolean changed;public void localeChangeListener(ValueChangeEvent e) {changed = true; // notify subscribersEventBusProvider.getEventBus().post(new LocaleChangeEvent(e.getNewValue()));}public void timeZoneChangeListener(ValueChangeEvent e) {changed = true; // notify subscribersEventBusProvider.getEventBus().post(new TimeZoneChangeEvent(e.getNewValue()));}public String saveUserSettings() {...if (changed) {// notify subscribersEventBusProvider.getEventBus().post(new SettingsChangeEvent());return "home";}}
}
- 事件生产者和事件观察者彼此分离。
- 观察者可以指定“选择器”的组合来缩小他们将接收的事件通知的范围。
- 可以立即或延迟通知观察者,直到当前事务结束为止。
- 使用条件观察者方法进行作用域定义时不会感到头痛(还记得作用域bean和Mediator / EventBus的问题吗?)。
public MyBean implements Serializable {public void onLocaleChangeEvent(@Observes Locale locale) {...}
}
如果观察者方法仅对限定的事件感兴趣,则事件参数也可以指定限定符-这些事件具有限定符。
public void onLocaleChangeEvent(@Observes @Updated Locale locale) {...
}
事件限定符只是使用@Qualifier定义的普通限定符。 这是一个例子:
@Qualifier
@Target({FIELD, PARAMETER})
@Retention(RUNTIME)
public @interface Updated {}
事件生产者使用参数化Event接口的实例触发事件。 该接口的实例通过注入获得。 生产者通过调用Event接口的fire()方法并传递事件对象来引发事件。
public class UserSettingsForm implements Serializable {@Inject @Any Event<Locale> localeEvent;public void localeChangeListener(ValueChangeEvent e) {// notify all observerslocaleEvent.fire((Locale)e.getNewValue());}
}
// this will raise events to observers having parameter @Observes @Updated Locale
@Inject @Updated Event<Locale> localeEvent;
public void onLocaleChangeEvent(@Observes @Updated Locale locale, User user) {...
}
动态指定限定符是什么? CDI允许通过AnnotationLiteral获得适当的限定符实例。 这样,我们可以将限定符传递给Event的select()方法。 例:
public class DocumentController implements Serializable {Document document;@Inject @Updated @Deleted Event<Document> documentEvent;public void updateDocument() {...// notify observers with @Updated annotationdocumentEvent.select(new AnnotationLiteral<Updated>(){}).fire(document);}public void deleteDocument() {...// notify observers with @Deleted annotationdocumentEvent.select(new AnnotationLiteral<Deleted>(){}).fire(document);}
}
public void onLocaleChangeEvent(@Observes(receive = IF_EXISTS) @Updated Locale locale) {...
}
翻译自: https://www.javacodegeeks.com/2012/07/jsf-event-based-communication-new.html
JSF基于事件的交流:新派方法相关推荐
- 基于jsf的项目_JSF基于事件的交流:新派方法
基于jsf的项目 在上一篇文章中 ,我们学习了基于Observer / Event Listener和Mediator模式的基于事件的通信. 由于它们的缺点,我想展示基于事件的通信的更有效方法. 我们 ...
- JSF基于事件的沟通:过时的方法
用JSF编写的Web应用程序由相互交互的bean组成. 在开发Web应用程序时,bean之间的通信是主要的设计模式之一. 有时,一个bean需要向其他bean发送事件,以通知它们某些更改或其他任何更改 ...
- 基于jsf的项目_JSF基于事件的沟通:过时的方法
基于jsf的项目 用JSF编写的Web应用程序由相互交互的bean组成. 在开发Web应用程序时,bean之间的通信是主要的设计模式之一. 有时,一个bean需要将事件发送给其他bean,以通知它们某 ...
- 基于HLW8110的交流测量原理及校准方法
基于HLW8110的交流测量原理及校准方法 ...
- 基于HLW8112的交流测量原理及校准方法
基于HLW8112的交流测量原理及校准方法 ...
- 激光雷达角点检测 c语言,一种基于事件帧的角点检测方法与流程
本发明属于图像处理领域,用于解决基于事件相机的SLAM项目的角点检测. 背景技术: 在过去几十年里,由于计算机视觉算法的研究与发展,人们对机器人感知的兴趣也日益增加.这种传统相机能够捕获相机周围环境的 ...
- 港科大开源 | 基于事件的双目视觉里程计
点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 1 摘要 本文提出了一种双目事件相机的视觉里程计方法.我们的系统遵循并行跟踪和建图的方法,建图模块以概 ...
- [译] 基于事件流构建的服务
[译] 基于事件流构建的服务 摘要:本文属于原创,欢迎转载,转载请保留出处:https://github.com/jasonGeng88/blog 原文:https://www.confluent.i ...
- 最新综述 | 基于深度学习的SLAM方法:面向空间机器智能时代
最新综述 | 基于深度学习的SLAM方法:面向空间机器智能时代 A Survey on Deep Learning for Localization and Mapping Towards the A ...
最新文章
- OpenAI联合创始人:AI的极限?我真的不知道!
- Dell R720上的系统安装问题的解决办法(关于RAID建立磁盘阵列的技术)
- 马云不会因支付宝事件走下神坛
- 外贸网络推广浅谈蜘蛛抓取频次的原则跟哪些有关?
- python怎么学好-怎么短时间学好python
- spring boot使用外置tomcat部署需要排除的依赖
- Juint整合Log4j
- 每天一道LeetCode-----回文链表
- 使用dubbo后尽量不用要@Reference可能引起冲突
- Python zmq 讲解
- linux中配置vsftpd
- 酷派D530刷机指引之民间ROM
- 机器学习中数据标准化相关方法
- 引擎国产化,VGS引领三维引擎新时空
- TwinCAT 3 使用XML-server
- 用VBA下载google图片
- 微信开放平台认证后怎么实现扫码登录功能
- dll 不是 PML.NET callable问题解决办法
- 机器学习Machine Learning
- 在线JSON转TSV工具
热门文章
- 局部描述符表LDT的作用+定义+初始化+跳转相关
- ConcurrentHashMap--自用,非教学
- java程序设置jvm_Java程序员应在2018年学习的3种JVM语言
- java agent_如何脚踏实地构建Java Agent
- 摆脱冷气_摆脱匿名类
- java minor gc_Java Minor发布计划再次进行了调整
- neo4j cypher_Neo4j:Cypher –避免热切
- 客户端渲染换为服务器端渲染_服务器与客户端渲染(AngularJS与服务器端MVC)
- Eclipse GlassFish 5.1就在这里!
- play框架配置 拦截器_如何使用Play框架为https配置SSL证书