在上篇博客 用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观察者模式灵活性的更多相关文章

  1. java 观察者模式 与spring配置

    一.Observer模式的意图: 在对象的内部状态发生变化时,自动通知外部对象进行响应. 二.Observer模式的构成: ·被观察者:内部状态有可能被改变,而且又需要通知外部的对象 ·观察者:需要对 ...

  2. 为什么选择 Spring 作为 Java 框架

    1. 概述 在本文中,我们将讨论 Spring 作为最流行的 Java 框架之一的主要价值体现. 最重要的是,我们将尝试理解 Spring 成为我们选择框架的原因.Spring 的详细信息及其组成部分 ...

  3. Spring 中的观察者模式

    一.Spring 中观察者模式的四个角色 1. 事件(ApplicationEvent) ApplicationEvent 是所有事件对象的父类.ApplicationEvent 继承自 jdk 的 ...

  4. SPRING SECURITY JAVA配置:Web Security

    在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...

  5. Java面试题系列 提高Java I/O 性能

    1.提高java的 i/o性能.. http://blog.csdn.net/cherami/article/details/3854 我们知道Java中一般的输入输出流都是用单字节的读取方法来进行I ...

  6. 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 ...

  7. 提高Java代码质量的Eclipse插件之Checkstyle的使用详解

    提高Java代码质量的Eclipse插件之Checkstyle的使用详解 CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发人员遵守某些编码规范的工具.它能够自动化代 ...

  8. Spring 基于Java的Bean声明

    Spring 基于Java的Bean声明 使用@Configuration进行设置: Xml: <?xml version="1.0" encoding="UTF- ...

  9. spring事件驱动模型--观察者模式在spring中的应用

    spring中的事件驱动模型也叫作发布订阅模式,是观察者模式的一个典型的应用,关于观察者模式在之前的博文中总结过,http://www.cnblogs.com/fingerboy/p/5468994. ...

随机推荐

  1. CentOS6.7 下安装git

    在CentOS5的时代,由于yum源中没有git,所以需要预先安装一系列的依赖包.但在CentOS6的yum源中已经有git的版本了,可以直接使用yum源进行安装. $ sudo yum instal ...

  2. python模块基础之getpass模块

    getpass模块提供了可移植的密码输入,一共包括下面两个函数: 1. getpass.getpass() 2. getpass.getuser() getpass.getpass([prompt[, ...

  3. jquery 中ajax的参数

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...

  4. rpm软件包类型

    rpm软件包 在linux世界里有两种流行的包管理方式,分别是redhat系的rpm和debian系的deb.其中rpm是RedHat Package Manager(RedHat软件包管理工具)的简 ...

  5. 使用ICallbackEventHandler接口更高效实现Ajax

    使用ICallbackEventHandler接口可以方便地高效地实现Ajax功能 1.处理页面需实现ICallbackEventHandler接口,此接口有两个方法 a.GetCallbackRes ...

  6. [c#]asp.net开发微信公众平台(5)微信图文消息

    上篇已经成功响应了关注事件,也实现了文本消息的发送,这篇开始图文消息处理, 微信中最常用的消息类型就是图文消息了,因为它图文并茂,最能表达信息. 图文消息在微信中的接口定义如下: <xml> ...

  7. 3 windows环境与shell交互操作

    /** * 由SshConfig配置获取一个Session * @param conf * @return */ public static Session createSession(SshConf ...

  8. 获取GET/POST提交的数据,并处理中文问题

    1.获取input标签中的值,用request.getParameter("User")(User为input的name值) 2. 获取checkbox的值,由于是多选的,所以不能 ...

  9. Codeforces Round #278 (Div. 1)

    A A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang an ...

  10. Width vs Pitch

    1.单位不同,width是像素,pitch是字节.因此一个640*480的8位图和640*480的32位 图他们width一样而pitch不一样. 2.pitch可能大于width个像素所占字节数.w ...