根据Martin Fowler所写的论文,控制反转是程序控制流反转的原理:由外部源(框架、服务、其他组件)控制程序流,而不是由程序员控制程序流。就像我们把一个东西插入另一个东西。他提到了一个关于EJB 2.0的例子:

例如,会话Bean接口 定义ejbRemove, ejbPassivate (存储到二级存储),以及 ejbActivate(从被动恢复 状态)。你不能控制时间 这些方法被调用,只是什么 他们做的事。集装箱召唤我们,我们 别这么说。

这就导致了framework和library的区别:

控制反转是一个关键的部分 框架与框架的区别是什么 图书馆。图书馆本质上是一个 一组你可以调用的函数, 这些日子通常组织成 类。每次调用都要做一些工作 将控制权返回给客户端。

我认为,DI是IOC的观点,意味着对象的依赖关系是倒置的:而不是它控制自己的依赖关系、生命周期……其他东西可以帮你。但是,正如你告诉我的DI by hands, DI不一定是IOC。我们仍然可以有DI,没有IOC。

然而,在本文中(来自pococapsule, C/ c++的另一个IOC框架),它指出,由于IOC和DI, IOC容器和DI框架要比J2EE优越得多,因为J2EE将框架代码混合到组件中,因此它不是普通的旧Java/ c++对象(POJO/POCO)。

依赖注入模式以外的控制反转容器(存档链接)

附加阅读,了解旧的基于组件的开发框架的问题,这导致了上面的第二篇论文:为什么和什么反转控制(档案链接)

我的问题:IOC和DI到底是什么?我很困惑。基于pococapsule, IOC不仅仅是对象或程序员与框架之间控制的反转。


当前回答

IoC(控制反转):这是一个通用术语,以多种方式实现(事件,委托等)。

依赖注入:依赖注入是IoC的一个子类型,通过构造函数注入、setter注入或接口注入实现。

但是,Spring只支持以下两种类型:

Setter Injection Setter-based DI is realized by calling setter methods on the user’s beans after invoking a no-argument constructor or no-argument static factory method to instantiate their bean. Constructor Injection Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.Using this we can validate that the injected beans are not null and fail fast(fail on compile time and not on run-time), so while starting application itself we get NullPointerException: bean does not exist. Constructor injection is Best practice to inject dependencies.

其他回答

IoC(控制反转):这是一个通用术语,以多种方式实现(事件,委托等)。

依赖注入:依赖注入是IoC的一个子类型,通过构造函数注入、setter注入或接口注入实现。

但是,Spring只支持以下两种类型:

Setter Injection Setter-based DI is realized by calling setter methods on the user’s beans after invoking a no-argument constructor or no-argument static factory method to instantiate their bean. Constructor Injection Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.Using this we can validate that the injected beans are not null and fail fast(fail on compile time and not on run-time), so while starting application itself we get NullPointerException: bean does not exist. Constructor injection is Best practice to inject dependencies.

DI是IoC的一个子集

IoC means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside service (for example, xml file or single app service). 2 implementations of IoC, I use, are DI and ServiceLocator. DI means the IoC principle of getting dependent object is done without using concrete objects but abstractions (interfaces). This makes all components chain testable, cause higher level component doesn't depend on lower level component, only from the interface. Mocks implement these interfaces.

以下是一些实现IoC的其他技术。

控制反转是一种设计范式,其目标是为应用程序的目标组件提供更多的控制,即完成工作的组件。 依赖注入是一种模式,用于创建其他对象所依赖的对象实例,而在编译时不知道将使用哪个类来提供该功能。

实现控制反转有几种基本技术。这些都是:

使用工厂模式 使用服务定位器模式 使用任何给定类型的依赖注入: 1).构造函数注入 2). setter注入 3).接口注入

IOC(控制反转):将获取对象实例的控制权交给容器称为控制反转,这意味着不是您使用new操作符创建对象,而是让容器为您创建对象。

DI(依赖注入):向对象注入属性的方式称为依赖注入。

我们有三种类型的依赖注入:

构造函数注入 Setter和Getter注入 接口注入

Spring只支持构造函数注入和Setter/Getter注入。

让我们从SOLID的D开始,看看Scott Millett的书《Professional ASP. IoC》中的DI和IoC。NET设计模式":

Dependency Inversion Principle (DIP) The DIP is all about isolating your classes from concrete implementations and having them depend on abstract classes or interfaces. It promotes the mantra of coding to an interface rather than an implementation, which increases flexibility within a system by ensuring you are not tightly coupled to one implementation. Dependency Injection (DI) and Inversion of Control (IoC) Closely linked to the DIP are the DI principle and the IoC principle. DI is the act of supplying a low level or dependent class via a constructor, method, or property. Used in conjunction with DI, these dependent classes can be inverted to interfaces or abstract classes that will lead to loosely coupled systems that are highly testable and easy to change. In IoC, a system’s flow of control is inverted compared to procedural programming. An example of this is an IoC container, whose purpose is to inject services into client code without having the client code specifying the concrete implementation. The control in this instance that is being inverted is the act of the client obtaining the service.

米勒特,C(2010)。专业的ASP。NET设计模式。威利出版社,7-8。