`
flm_llx
  • 浏览: 60357 次
  • 性别: Icon_minigender_1
  • 来自: 应县
社区版块
存档分类
最新评论

spring:Inversion of Control具体实现【资料搜集】

    博客分类:
  • java
阅读更多
IoC(Inversion of Control)控制反转的具体表现为以下几点:
    高层模块不应该依赖底层模块,而是模块都必须依赖于抽象。
     实现必须依赖抽象,而不是抽象依赖实现。
     如果高层模块直接执行底层模块的函数,就对底层模块产生了依赖关系。
在设计上希望模块都依赖与模块的抽象,这样才可以重用高层的应用程序设计。
public class Business{
    private FloppyWriter writer = new FloppyWriter();
    ...
    public void save(){
        ...
        writer.saveToFloppy();
    }   
}
    Business类的设计中,存盘的需求依赖于实际的FloppyWriter对象,如果今天想要将存储介质换为USB磁盘,则必须修改Business的程序,您无法直接重复使用Business类。
    如果透过接口的声明,可以改进这种情况,例如可先定义一个IDeviceWriter接口:
public interface IDeviceWriter{
    public void saveToDevice();   
}
    接着所设计的Business类,在遇到存盘需求时,可以设计为依赖与IDevice接口,而不是依赖于实际的FloppyWriter,例如:
public class Business{
    private IDeviceWriter writer;
   
    public void setDeviceWriter(IDeviceWriter writer){
        this.writer = writer;   
    }
   
    public void save(){
        ...
        writer.saveToDevice();
    }
}
    在这样的设计下,Business类就是可以重用的,如果今天有存储至Floppy或USB磁盘的需求,只要针对这两种储存需求分别实现接口即可,例如针对Floppy存储设计一个FloppyWriter类:
public class FloppyWriter implements IDeviceWriter{
    public void saveToDevice(){
        ...
        //实际存储至Floppy的程序代码
    }
}
    或是针对USB磁盘存储设计一个UsbDiskWriter类:
public class UsbDiskWriter implements IDeviceWriter{
    public void saveToDevice(){
        ...
        //实际存储至UsbDisk的程序代码
}
如果应用程序需要Floppy存储的话,可以编写一个配置程序如下:
Business business = new Business();
business.setDeviceWriter(new FloppyWriter());
business.save();
同样的,如果应用程序需要USB磁盘的话,可以编写一个配置程序如下:
Business business = new Business();
business.setDeviceWriter(new UsbDiskWriter());
business.save();
    IoC要求的是容器不应该(或尽量不要)侵入应用程序,也就是不应该出现与容器相依的API,应用程序本身可以依赖于抽象的接口,容器可以透过这些抽象接口将所需的资源注入至应用程序中,应用程序不向容器主动要求资源,故而不会依赖于容器的特定API,应用程序本身不会意识到正被容器使用,可以随时从容器系统中脱离,转移至其他的容器或框架而不用作任何的修改。
    IoC模式基本上是一个高层的模式概念,在Martin Fowler的Inversion of Control Containers and the Dependency Injection pattern中谈到,实现IoC有两种方式:Dependency Injection 与Service Locator,Spring所采用的是Dependency Injection来实现IoC,中文翻译为依赖注入。
    Spring的核心是个IoC容器,可以用Setter或构造函数的方式来实现您的应用程序对象,至于对象与独享之间的关系建立,则可以透过配置文件设定(一个XML文件或是一个.properies文件),让Spring在执行时期根据配置文件的设定,为您建立对象之间的依赖关系,您不必特地编写一些程序来自行建立这些对象之间的依赖关系,这不仅减少大量的程序编写,也降低了对象之间的耦合程度。
分享到:
评论

相关推荐

    Inversion of Control Containers and the Dependency Injection pattern.docx

    Inversion of Control Containers and the Dependency Injection pattern(中文版)

    Event-Based Programming without Inversion of Control.pdf

    Event-Based Programming without Inversion of Control-jmlc06.pdf

    控制反轉(Inversion of Control)

    控制反轉(Inversion of Control),是近來較為新興的系統開發模式,它的特性可使架構顯得更為鬆散,更驅進OO的核心思維。

    Learning.Spring.Application.Development.1783987367

    Chapter 2: Inversion Of Control In Spring Chapter 3: Dao And Jdbc In Spring Chapter 4: Hibernate With Spring Chapter 5: Spring Web Mvc Framework Chapter 6: Spring Security Chapter 7: Spring Testing ...

    IoC_Spring.rar_inversion_spring

    Inversion of control with Spring

    IOC(inversion of control)

    NULL 博文链接:https://moshalanye.iteye.com/blog/840421

    Apress.Introducing.Spring.Framework.A.Primer.

    This book is an introduction to the well-known Spring Framework that offers an inversion of control container for the Java platform. The Spring Framework is an open source application framework that ...

    spring_note.rar_inversion_spring concept

    inversion of control dependency injection AOP的概念与好处 Spring简介 Spring应用IOC/DI(重要) xml annotation Spring应用AOP(重要) xml annotation Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要)...

    SpringSecurity.zip

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    SpringSecutiry实现认证授权功能,整合SpringBoot

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    java面试Spring.pdf

    IOC,Inversion of Control,控制反转 AOP(Aspect-OrientedProgramming),面向切面编程 Spring AOP里面常用名词的概念: Spring容器的启动流程 Spring Bean的生命周期? Spring中bean的作用域 说一下Spring基于xml...

    Spring Security、Spring Social 、Spring Security OAuth

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    spring jar包

    3.spring-beans:基础jar包,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI 支持,引入spring-core.jar 及spring...

    Pro Spring 3

    What Inversion of Control (IoC) and dependency injection (DI) are Aspect-oriented programming techniques with Spring, and why they’re important Data access and persistence using Spring and Hibernate,...

    Spring框架文档 - 核心技术部分 中文版 - 第一部分.pdf

    其中最重要的是Spring框架的控制反转(Inversion of Control,IoC)容器(container)。在对Spring框架的IoC容器进行全面介绍之后,将全面介绍Spring的面向切面编程(AOP)技术。Spring框架(Spring Framework)有...

    CornPrincess#Backend_Notes#深入浅出Ioc1

    引入Ioc: Inversion of Control 中文名即控制反转,这是一种设计思想,有点类似于设计模式,并不是一门具体的技术。其核心为将你设计好的对象由

    Spring框架文档 - 核心技术部分 中英双语版 第一部分.pdf

    其中最重要的是Spring框架的控制反转(Inversion of Control,IoC)容器(container)。在对Spring框架的IoC容器进行全面介绍之后,将全面介绍Spring的面向切面编程(AOP)技术。Spring框架(Spring Framework)有...

    Spring框架文档 - 核心技术部分 中英双语版 第二部分.pdf

    其中最重要的是Spring框架的控制反转(Inversion of Control,IoC)容器(container)。在对Spring框架的IoC容器进行全面介绍之后,将全面介绍Spring的面向切面编程(AOP)技术。Spring框架(Spring Framework)有...

    Spring框架文档 - 核心技术部分 中文版 - 第二部分.pdf

    其中最重要的是Spring框架的控制反转(Inversion of Control,IoC)容器(container)。在对Spring框架的IoC容器进行全面介绍之后,将全面介绍Spring的面向切面编程(AOP)技术。Spring框架(Spring Framework)有...

Global site tag (gtag.js) - Google Analytics