用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. ...
随机推荐
- [ES6] Objects create-shorthand && Destructuring
Creating Object: Example 1: let name = "Brook"; let totalReplies = 249; let avatar = " ...
- ASP.NET快速学习方案(.NET菜鸟的成长之路)
想要快速学习ASP.NET网站开发的朋友可以按照下面这个学习安排进度走.可以让你快速入门asp.net网站开发!但也局限于一般的文章类网站!如果想学习更多的技术可以跟着我的博客更新走!我也是一名.NE ...
- hdu 1301
最小生成树模板题 简单的prim算法 AC代码: #include <iostream> #include <stdio.h> #define INF 9999999 usin ...
- (转)使用Microsoft Web Application Stress Tool对web进行压力测试
http://www.blogjava.net/crespochen/archive/2009/06/02/279538.html Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试 ...
- 导入表数据 txt
导入表数据 txt mysql> load data infile "D:/import.txt" into table shop;输出: Query OK, rows af ...
- Repeater绑定数据库,使用AspNetPager进行分页
分页是Web中经常遇到的功能,分页主要有真分页和假分页. 所谓真分页是指:每一页显示多少数据,就从数据库读多少数据: 假分页是指:一次性从数据库读取所有数据,然后再进行分页. 这两种分页方式区别在于从 ...
- Activiti工作流学习-----基于5.19.0版本(5)
五.与Spring集成 实际项目中一般都有Spring的身影,与Spring集成使得Activiti的实用性得到提高.activiti和Spring整合需要activiti-spring的jar在类路 ...
- NSNotificationCenter基础知识
NSNotificationCenter基础知识 Notification的工作机制 1.对应用程序中其他地方发生的事件(比如增加一条数据库记录)感兴趣的对象,会向通告中心(Notificatio ...
- 关于炒股软件——金魔方炒股软件的Dll外挂开发
2015-01-19 14:40:04 金魔方平台是由飞狐交易师原创团队集多年研发经验,依靠和讯财经网强大资源,吸取国际专家思路而推出的十年巨作.目前新出的这个2.0版,这一版在数据存储方面作很大的改 ...
- 怎样在Word中插入代码并保持代码原始样式不变
怎样在Word中插入代码并保持样式不变 我们有时候需要在word中添加一段我们写的代码,但是把代码粘贴到word文档中之后就发现所有的代码的样子都变了,我们可以采用下边的方法来实现保持代码原来的样式和 ...