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 -- 松耦合的实现的更多相关文章

  1. C#进阶系列——MEF实现设计上的“松耦合”(一)

    前言:最近去了趟外地出差,介绍推广小组开发的框架类产品.推广对象是本部门在项目上面的同事——1到2年工作经验的初级程序员.在给他们介绍框架时发现很多框架设计层面的知识他们都没有接触过,甚至没听说过,这 ...

  2. C#进阶系列——MEF实现设计上的“松耦合”(二)

    前言:前篇 C#进阶系列——MEF实现设计上的“松耦合”(一) 介绍了下MEF的基础用法,让我们对MEF有了一个抽象的认识.当然MEF的用法可能不限于此,比如MEF的目录服务.目录筛选.重组部件等高级 ...

  3. C#进阶系列——MEF实现设计上的“松耦合”(四):构造函数注入

    前言:今天十一长假的第一天,本因出去走走,奈何博主最大的乐趣是假期坐在电脑前看各处堵车,顺便写写博客,有点收获也是好的.关于MEF的知识,之前已经分享过三篇,为什么有今天这篇?是因为昨天分享领域服务的 ...

  4. Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法

    Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...

  5. MEF实现设计上的“松耦合”

    C#进阶系列——MEF实现设计上的“松耦合”(二)   前言:前篇 C#进阶系列——MEF实现设计上的“松耦合”(一) 介绍了下MEF的基础用法,让我们对MEF有了一个抽象的认识.当然MEF的用法可能 ...

  6. spring3系列一

    IOC基础 Ioc是什么 Ioc--Inversion of Control 控制反转,不是什么技术,而是一种设计思想.在java开发中,ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对 ...

  7. Spring3系列13-Controller和@RequestMapping

    Spring3系列13-Controller和@RequestMapping Controller返回值,String或者ModelAndView @RequestMapping关联url @Requ ...

  8. Spring3系列12- Spring AOP AspectJ

    Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...

  9. Spring3系列11- Spring AOP——自动创建Proxy

    Spring3系列11- Spring AOP——自动创建Proxy 在<Spring3系列9- Spring AOP——Advice>和<Spring3系列10- Spring A ...

随机推荐

  1. 时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell

    时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell opensuse 一些常用命令:    service xxx start/s ...

  2. 编译nginx时提示undefined reference to 'pcre_free_study' 的问题及解决

    ./configure --add-module=../ngx_devel_kit-0.2.19/ --add-module=../lua-nginx-module-0.9.19/  --with-l ...

  3. ASP.NET Web API 跨域访问

    自定义特性 要在WebApi中实现JSONP,一种方式是实现自定义特性  http://stackoverflow.com/questions/9421312/jsonp-with-asp-net-w ...

  4. ArcMap 连接SDE 出错“Failed to connect to the specified server. Entry for SDE instance no found in services file.”

    问题描述 环境: ARCMAP 10.0 ARCSDE FOR ORACLE 10.0   在通过用ArcMap 连接ORACLE SDE时出现上面的错.   解决方式 在 C:\Windows\Sy ...

  5. 微软BI 之SSIS 系列 - MVP 们也不解的 Scrip Task 脚本任务中的一个 Bug

    开篇介绍 前些天自己在整理 SSIS 2012 资料的时候发现了一个功能设计上的疑似Bug,在 Script Task 中是可以给只读列表中的变量赋值.我记得以前在 2008 的版本中为了弄明白这个配 ...

  6. 拟物设计和Angular的实现 - Material Design(持续更新)

    Material Design是Google最新发布的跨平台统一视觉设计语言.直接翻译是物质设计,但是我更倾向于使用"拟物设计"更为准确. 据谷歌介绍,Material Desig ...

  7. Working With Taxonomy Field in CSOM

    How to create taxonomy field with CSOM If you need to programmatic create a taxonomy field, you need ...

  8. C#课外实践——校园二手平台(技术篇1)

    前面分享了这次的课外实践的心得,这次,就分享一下从这次的课外实践的过程中学到的知识技能吧.虽然有句话说的好,不要做没有准备的战争,但是,我想说的是,生活中有很多的事情是不允许我们有准备的.遇到事情必须 ...

  9. attilax.java 注解的本质and 使用最佳实践(3)O7

    attilax.java 注解的本质and 使用最佳实践(3)O7 1. 定义pojo 1 2. 建立注解By eclipse tps 1 3. 注解参数的可支持数据类型: 2 4. 注解处理器 2 ...

  10. MultiTouch————多点触控,伸缩图片,变换图片位置

    前言:当今的手机都支持多点触控功能(可以进行图片伸缩,变换位置),但是我们程序员要怎样结合硬件去实现这个功能呢? 跟随我一起,来学习这个功能 国际惯例:先上DEMO免费下载地址:http://down ...