用Spring提高java观察者模式灵活性
在上篇博客 用java观察者模式解耦经典三层架构 的最后,用了一个Client类把Listener的实现类注冊到了LoginEventSource类中,假设须要加入�新的逻辑,加入�新的listener类后,还是须要改动Client类,那么我们能够借助spring提供的容器来帮助我们完好观察者模式。
在spring,在src下建立spring配置文件
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean>
<!-- EventSource -->
<bean id="loginEventSource" class="com.tgb.chargeSystem.LoginEventSource">
</bean> <!-- Listener -->
<bean id="loginLogListener" class="com.tgb.chargeSystem.LoginLogListener">
</bean>
<bean id="loginVerificationListener" class="com.tgb.chargeSystem.LoginVerificationListener">
</bean> <bean id="registerLoginVerificationListener" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="loginEventSource" />
</property>
<property name="targetMethod">
<value>registerListener</value>
</property>
<property name="arguments">
<list>
<ref bean="loginVerificationListener" />
</list>
</property>
</bean> <bean id="registerLoginLogListener" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="loginEventSource" />
</property>
<property name="targetMethod">
<value>registerListener</value>
</property>
<property name="arguments">
<list>
<ref bean="loginLogListener" />
</list>
</property>
</bean>
通过MethodInvokingFactoryBean,我们能够通过配置文件把Listener注冊到相应的事件源,因此避免了在类中的硬编码。
而client代码则改为
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); LoginEventSource loginEventSource=(LoginEventSource)ctx.getBean("loginEventSource");
loginEventSource.notifyListenner();
用Spring提高java观察者模式灵活性的更多相关文章
- java 观察者模式 与spring配置
一.Observer模式的意图: 在对象的内部状态发生变化时,自动通知外部对象进行响应. 二.Observer模式的构成: ·被观察者:内部状态有可能被改变,而且又需要通知外部的对象 ·观察者:需要对 ...
- 为什么选择 Spring 作为 Java 框架
1. 概述 在本文中,我们将讨论 Spring 作为最流行的 Java 框架之一的主要价值体现. 最重要的是,我们将尝试理解 Spring 成为我们选择框架的原因.Spring 的详细信息及其组成部分 ...
- Spring 中的观察者模式
一.Spring 中观察者模式的四个角色 1. 事件(ApplicationEvent) ApplicationEvent 是所有事件对象的父类.ApplicationEvent 继承自 jdk 的 ...
- SPRING SECURITY JAVA配置:Web Security
在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...
- Java面试题系列 提高Java I/O 性能
1.提高java的 i/o性能.. http://blog.csdn.net/cherami/article/details/3854 我们知道Java中一般的输入输出流都是用单字节的读取方法来进行I ...
- struts2启动报错com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1
好久没有搞struts2,今天配置strut2.2.1,启动时遇到个小问题.记录下. tomcat启动报错: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- 提高Java代码质量的Eclipse插件之Checkstyle的使用详解
提高Java代码质量的Eclipse插件之Checkstyle的使用详解 CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发人员遵守某些编码规范的工具.它能够自动化代 ...
- Spring 基于Java的Bean声明
Spring 基于Java的Bean声明 使用@Configuration进行设置: Xml: <?xml version="1.0" encoding="UTF- ...
- spring事件驱动模型--观察者模式在spring中的应用
spring中的事件驱动模型也叫作发布订阅模式,是观察者模式的一个典型的应用,关于观察者模式在之前的博文中总结过,http://www.cnblogs.com/fingerboy/p/5468994. ...
随机推荐
- JS函数运行在它们被定义的作用域内,而不是它们被执行的作用域内
一个函数的作用域并不会因为被另一个函数调用而拓展,取得另一个函数的作用域: function show(name){ alert(name) } function show2(){ var name= ...
- haxe jni调用输入法
public static void startInputDialog(final String title, final String text, final String buttonLabel, ...
- BZOJ1951[SDOI2010]古代猪文
Description "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久 ...
- 推荐几个的chorme的扩展程序
下面来解释下这几个插件 (0)Context : 管理这些乱七八糟的扩展程序,插件用的时候,点开,不用的时候,关闭,这样可以降低游览器的负荷 (1)AngularJS Batarang : 测试ang ...
- WEB数据挖掘(十三)——关联规则与序列模式(1)
一.Apriori算法简介: Apriori算法是一种挖掘关联规则的频繁项集算法,其核心思想是通过候选集生成和情节的向下封闭检测两个阶段来挖掘频繁项集. Apriori(先验的,推测的)算法应用广泛 ...
- Reason: image not found 错误解决方法
dyld: Library not loaded: /System/Library/Frameworks/Social.framework/Social Referenced from: /var ...
- Jmeter代理服务器设置
上一篇我们讲了代理工作的原理,这次我们就来分享下,如何来设置代理. (一)设置Jmeter之web代理,操作步骤这里就直接用动图代替了. 第一步:设置Jmeter的代理,添加Http代理服务器 第二步 ...
- BZOJ3644 : 陶陶的旅行计划
假设是序列问题,且$S<T$,可以贪心求解,通过维护下述信息进行区间合并. 对于区间$[l,r]$,维护的信息有: $v$:跳到了$\geq r$的位置后,可以花费$1$往右最多扩展多少. $f ...
- 转载:如何搭建turn server 在centos7上。
https://www.cnblogs.com/idignew/p/7440048.html
- 4.翻译系列:EF 6 Code-First默认约定(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/code-first-conventions.aspx EF 6 Code-First系列 ...