zoukankan      html  css  js  c++  java
  • 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......”,结果如下图

     

  • 相关阅读:
    December 23rd 2016 Week 52nd Friday
    December 22nd 2016 Week 52nd Thursday
    December 21st 2016 Week 52nd Wednesday
    December 20th 2016 Week 52nd Tuesday
    December 19th 2016 Week 52nd Sunday
    December 18th 2016 Week 52nd Sunday
    uva294(唯一分解定理)
    uva11624Fire!(bfs)
    fzu2150Fire Game(双起点bfs)
    poj3276Face The Right Way
  • 原文地址:https://www.cnblogs.com/jcomet/p/5570436.html
Copyright © 2011-2022 走看看