Spring3系列2 -- 松耦合的实现
Spring3系列2 -- 松耦合的实现
一、 环境
spring-framework-3.2.4.RELEASE
jdk1.7.0_11
Maven3.0.5
eclipse-jee-juno-SR2-win32
此例不必重复创建项目,上接Spring3-Example项目。
二、 pom.xml文件配置
与前一个项目Spring3-Example中的pom.xml文件一致,不必修改。
三、 创建输出类Output Generator类
假设你的项目需要输出到CVS或者JSON,创建以下类。
文件1:IOutputGenerator.java 一个output接口
- package com.lei.demo.loosely_coupled;
- public interface IOutputGenerator {
- public void generateOutput();
- }
文件2:CsvOutputGenerator.java CVS输出,实现了IOutputGenerator接口
- package com.lei.demo.loosely_coupled;
- public class CsvOutputGenerator implements IOutputGenerator {
- public void generateOutput() {
- System.out.println("输出CsvOutputGenerator生成 Output......");
- }
- }
文件3:JsonOutputGenerator.java JSON输出,实现了IOutputGenerator接口
- package com.lei.demo.loosely_coupled;
- public class JsonOutputGenerator implements IOutputGenerator {
- public void generateOutput() {
- System.out.println("输出JsonOutputGenerator生成 Output......");
- }
- }
四、 用Spring依赖注入调用输出
用Spring的松耦合实现输出相应的格式。
首先创建一个需要用到输出的类OutputHelper.java
- package com.lei.demo.loosely_coupled;
- public class OutputHelper {
- IOutputGenerator outputGenerator;
- public void generateOutput(){
- this.outputGenerator.generateOutput();
- }
- public void setOutputGenerator(IOutputGenerator outputGenerator){
- this.outputGenerator = outputGenerator;
- }
- }
五、 创建一个spring配置文件用于依赖管理
src/main/resources下创建配置文件Spring-Output.xml。
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="OutputHelper" class="com.lei.demo.loosely_coupled.OutputHelper">
- <property name="outputGenerator" ref="CsvOutputGenerator" />
- </bean>
- <bean id="CsvOutputGenerator" class="com.lei.demo.loosely_coupled.CsvOutputGenerator" />
- <bean id="JsonOutputGenerator" class="com.lei.demo.loosely_coupled.JsonOutputGenerator" />
- </beans>
六、 通过Spring调用相应的output
App.java类
- package com.lei.demo.loosely_coupled;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class App {
- private static ApplicationContext context;
- public static void main(String[] args){
- context = new ClassPathXmlApplicationContext(new String[] {"Spring-Output.xml"});
- OutputHelper output = (OutputHelper)context.getBean("OutputHelper");
- output.generateOutput();
- }
- }
现在,已经实现了松耦合,当需要输出改变时,不必修改任何代码.java文件,只要修改Spring-Output.xml文件<property name="outputGenerator" ref="CsvOutputGenerator" />中的ref值,就可以实现输出不同的内容,不修改代码就减少了出错的可能性。
七、 目录结构
八、 运行结果
运行以上App.java,“输出CsvOutputGenerator生成 Output......”,结果如下图
Spring3系列2 -- 松耦合的实现的更多相关文章
- C#进阶系列——MEF实现设计上的“松耦合”(一)
前言:最近去了趟外地出差,介绍推广小组开发的框架类产品.推广对象是本部门在项目上面的同事——1到2年工作经验的初级程序员.在给他们介绍框架时发现很多框架设计层面的知识他们都没有接触过,甚至没听说过,这 ...
- C#进阶系列——MEF实现设计上的“松耦合”(二)
前言:前篇 C#进阶系列——MEF实现设计上的“松耦合”(一) 介绍了下MEF的基础用法,让我们对MEF有了一个抽象的认识.当然MEF的用法可能不限于此,比如MEF的目录服务.目录筛选.重组部件等高级 ...
- C#进阶系列——MEF实现设计上的“松耦合”(四):构造函数注入
前言:今天十一长假的第一天,本因出去走走,奈何博主最大的乐趣是假期坐在电脑前看各处堵车,顺便写写博客,有点收获也是好的.关于MEF的知识,之前已经分享过三篇,为什么有今天这篇?是因为昨天分享领域服务的 ...
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...
- MEF实现设计上的“松耦合”
C#进阶系列——MEF实现设计上的“松耦合”(二) 前言:前篇 C#进阶系列——MEF实现设计上的“松耦合”(一) 介绍了下MEF的基础用法,让我们对MEF有了一个抽象的认识.当然MEF的用法可能 ...
- spring3系列一
IOC基础 Ioc是什么 Ioc--Inversion of Control 控制反转,不是什么技术,而是一种设计思想.在java开发中,ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对 ...
- Spring3系列13-Controller和@RequestMapping
Spring3系列13-Controller和@RequestMapping Controller返回值,String或者ModelAndView @RequestMapping关联url @Requ ...
- Spring3系列12- Spring AOP AspectJ
Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...
- Spring3系列11- Spring AOP——自动创建Proxy
Spring3系列11- Spring AOP——自动创建Proxy 在<Spring3系列9- Spring AOP——Advice>和<Spring3系列10- Spring A ...
随机推荐
- Nhibernate的介绍
1.介绍的内容 1.感谢园友的文章支持 by 李永京 by wolfy 2.Nhibernate的框架介绍 3.Nhibernate的架构介绍 4.Nhibernate映射方法介绍(该点自己也存在一定 ...
- [ACM_图论] ZOJ 3708 [Density of Power Network 线路密度,a->b=b->a去重]
The vast power system is the most complicated man-made system and the greatest engineering innovatio ...
- 浅谈Entity Framework中的数据加载方式
如果你还没有接触过或者根本不了解什么是Entity Framework,那么请看这里http://www.entityframeworktutorial.net/EntityFramework-Arc ...
- JavaScript事件详解-zepto的事件实现
zepto的event 可以结合上一篇JavaScript事件详解-原生事件基础(一)综合考虑源码暂且不表,github里还有中文网站都能下到最新版的zepto.整个event模块不长,274行,我们 ...
- 易出错的C语言题目之一:宏定义与预处理
1.写出下列代码的运行结果: #include<stdio.h> #include<string.h> #define STRCPY(a,b) strcpy(a##_p,#b) ...
- paip.Java Annotation注解的作用and 使用
paip.Java Annotation注解的作用and 使用 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog. ...
- 盘点mysql中容易被我们误会的地方
引语:mysql作为数据库的一大主力军,到处存在于我们各种系统中,相信大家都不陌生!但是,你知道你能用不代表你知道细节,那我们就来盘点盘点其中一些我们平时不太注意的地方,一来为了有趣,二来为了不让自己 ...
- JavaWeb学习总结(十二)——Session
一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...
- node.js WebService异常处理(domain)以及利用domain实现request生命周期的全局变量
成熟的Web Service技术,例如Fast CGI.J2EE.php,必然会对代码异常有足够的保护,好的Web必然会在出错后给出友好的提示,而不是莫名其妙的等待504超时.而node.js这里比较 ...
- Python错误、调试和测试
try: print('try...') r = 10 / int('a') print('result:', r) except ValueError as e: print('ValueError ...