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......”,结果如下图

     

  • 相关阅读:
    线程简介
    JDBC连接数据库遇到的“驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。
    项目开发中需要考虑的问题2
    JPA使用中遇到Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: XXX is not mapped
    ORA-01747: user.table.column, table.column 或列说明无效
    IDEA使用一套代码启动多个应用
    tomcat8.5部署管理控制台
    ubuntu16虚拟机迁移/移动/复制后无法上网
    centos7没有IP地址
    repmgr自动故障转移的参数配置
  • 原文地址:https://www.cnblogs.com/jcomet/p/5570436.html
Copyright © 2011-2022 走看看