Java 控制反转与依赖注入的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26884881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:28:06  来源:igfitidea点击:

Difference between Inversion of Control & Dependency Injection

javaspringoopdesign-patterns

提问by Devendra Anchal

IoC and DI concept were very much confusing for me so I read lots of views and post of different people and finally reach to the conclusion. So as I understand these topics are...

IoC 和 DI 的概念让我很困惑,所以我阅读了很多不同人的观点和帖子,最后得出了结论。因此,据我所知,这些主题是...

Inversion of control is a technique in java for making loosely coupled and more easily maintainable applications, and dependency injection is a one of the way to achieve this concept(technique).

控制反转是java中一种用于制作松散耦合且更易于维护的应用程序的技术,而依赖注入是实现这一概念的一种方式(技术)。

Inversion of control container inject dependency at run time from java classes to make loosely coupled application.

控制反转容器在运行时从 java 类注入依赖关系,使应用程序松散耦合。

Is this true ? correct me if I am wrong...

这是真的 ?如果我错了,请纠正我...

回答by NG.

IoC is a generic term meaning rather than having the application call the methods in a framework, the framework calls implementations provided by the application.

IoC 是一个通用术语,意思不是让应用程序调用框架中的方法,而是框架调用应用程序提供的实现。

DI is a form of IoC, where implementations are passed into an object through constructors/setters/service look-ups, which the object will 'depend' on in order to behave correctly.

DI 是 IoC 的一种形式,其中实现通过构造函数/设置器/服务查找传递到对象中,对象将“依赖”以正确运行。

Reference : Inversion of Control vs Dependency Injection

参考:控制反转与依赖注入

回答by Adi Sembiring

Inversion of Control (IoC) refers to a programming style where a framework or runtime, controls the program flow. Inversion of control means we are changing the control from normal way. It works on Dependency Inversion Principle.

控制反转 (IoC) 是指一种编程风格,其中框架或运行时控制程序流。控制反转意味着我们正在改变正常方式的控制。它适用于依赖倒置原则。

DI is a software design pattern that allow us to develop loosely coupled code. DI is a great way to reduce tight coupling between software components. DI also enables us to better manage future changes and other complexity in our software. The purpose of DI is to make code maintainable.

DI 是一种软件设计模式,允许我们开发松散耦合的代码。DI 是减少软件组件之间紧密耦合的好方法。DI 还使我们能够更好地管理我们软件中的未来变化和其他复杂性。DI 的目的是使代码可维护。

回答by samuelj90

These are patterns to achieve loose coupling in java programming

这些是在java编程中实现松耦合的模式

DI(Dependency Injection):
Dependency injection is a pattern used to create instances of objects that other objects rely upon without knowing at compile time which class will be used to provide that functionality or simply the way of injecting properties to an object is called dependency injection.

DI(Dependency Injection)
依赖注入是一种模式,用于创建其他对象所依赖的对象的实例,而在编译时不知道将使用哪个类来提供该功能,或者简单地将属性注入对象的方式称为依赖注入.

We have three types of Dependency injection

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

  1. Constructor Injection
  2. Setter/Getter Injection
  3. Interface Injection
  1. 构造函数注入
  2. Setter/Getter 注入
  3. 接口注入

Spring will support only Constructor Injection and Setter/Getter Injection.

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

IOC(Inversion Of Control):
Giving control to the container to create and inject instances of objects that your application depend upon, means instead of you are creating an object using the newoperator, let the container do that for you. Inversion of control relies on dependency injection because a mechanism is needed in order to activate the components providing the specific functionality

IOC(控制反转):将
控制权交给容器以创建和注入应用程序所依赖的对象实例,这意味着不是您使用new操作符创建对象,而是让容器为您完成。控制反转依赖于依赖注入,因为需要一种机制来激活提供特定功能的组件

The two concepts work together in this way to allow for much more flexible, reusable, and encapsulated code to be written. As such, they are important concepts in designing object-oriented solutions.

这两个概念以这种方式协同工作,以允许编写更灵活、可重用和封装的代码。因此,它们是设计面向对象解决方案的重要概念。

Example for Dependency injection

依赖注入示例

Previously we are writing code like this

以前我们是这样写代码的

Public MyClass{
 DependentClass dependentObject
 /*
  At somewhere in our code we need to instantiate 
  the object with new operator  inorder to use it or perform some method.
  */ 
  dependentObject= new DependentClass();
  dependentObject.someMethod();
}

With Dependency injection, the dependency injector will take off the instantiation for us

通过依赖注入,依赖注入器会为我们完成实例化

Public MyClass{
 /* Dependency injector will instantiate object*/
 DependentClass dependentObject

 /*
  At somewhere in our code we perform some method. 
  The process of  instantiation will be handled by the dependency injector
 */ 

  dependentObject.someMethod();
}

The above process of giving the control to some other (for example the container) for the instantiation and injection can be termed as Inversion of Control

上述将控制权交给其他一些(例如容器)以进行实例化和注入的过程可以称为控制反转

You can read more on dependency injection and IOC in my answer :- You can find advantages and applications of the concepts here.

您可以在我的回答中阅读有关依赖项注入和 IOC 的更多信息:-您可以在此处找到这些概念的优点和应用。

What is dependency injection?

什么是依赖注入?

回答by Nico

Inversion of control means the program delegates control to someone else who will drive the flow IOC (Inversion of control) is a general parent term while DI (Dependency injection) is a subset of IOC. IOC is a concept where the flow of application is inverted. The control of the logic which is not part of that entity is taken by someone else. DI provides objects that an object needs. So rather than the dependencies construct themselves they are injected. The biggest benefit achieved by the above approach is “Decoupling”, we can invoke an object and pass any object keeping objects independent improving reusability and maintenance.

控制反转意味着程序将控制委托给将驱动流程的其他人 IOC(控制反转)是一个通用的父术语,而 DI(依赖注入)是 IOC 的一个子集。IOC 是一个应用流程倒置的概念。不属于该实体的逻辑的控制权由其他人控制。DI 提供对象需要的对象。因此,它们被注入而不是依赖构建本身。上述方法实现的最大好处是“解耦”,我们可以调用一个对象并传递任何对象,保持对象独立,提高可重用性和可维护性。

回答by user8293791

IOC stands for "Inversion of Control". It will works based on IOC principle,it means collaborating the objects and managing the objects of life cycle.Collaborating means the objects are group together at one place. we can collaborating the objects are two ways which they are Dependency Pull and Dependency Injection . DP divided into two ways (Dependency pull and Contextual Dependency pull) and DI also divided into two ways (Setter Injection and Constructor Injection).IOC supports both the ways but,the main intention of spring frame work is our components will be come completely loosely coupled. If we will use Dependency pull our component class will become tightly coupled with another class even though as part of spring frame work.then the recommended to go for Dependency Injection (Setter Injection or Constructor Injection). There is no difference between the IOC and DI.IOC is different and DI is different, DI is one of the part of IOC.

IOC 代表“控制反转”。它将基于IOC原理工作,这意味着协作对象和管理生命周期的对象。协作意味着对象在一个地方组合在一起。我们可以通过两种方式协作对象,它们是依赖拉取和依赖注入。DP 分为两种方式(Dependency pull 和 Contextual Dependency pull),DI 也分为两种方式(Setter Injection 和 Constructor Injection)。IOC 支持这两种方式,但是 Spring 框架的主要意图是我们的组件将完全松散耦合。如果我们使用 Dependency pull,我们的组件类将与另一个类紧密耦合,即使作为 spring 框架工作的一部分。那么推荐使用依赖注入(Setter 注入或构造函数注入)。

回答by Krishnanunni P V

Inversion of Control and Dependency Injection is a core design pattern of Spring framework. IOC and DI design pattern is also a popular design pattern interview question in Java. As the name suggest Inversion of control pattern Inverts responsibility of managing the life cycle of the object e.g. creating an object, setting their dependency etc from application to a framework, which makes writing Java application even more easy.

控制反转和依赖注入是 Spring 框架的核心设计模式。IOC 和 DI 设计模式也是 Java 中流行的设计模式面试题。顾名思义,控制模式反转 反转管理对象生命周期的责任,例如创建对象、设置它们的依赖等从应用程序到框架,这使得编写 Java 应用程序更加容易。

Read more: http://javarevisited.blogspot.com/2012/12/inversion-of-control-dependency-injection-design-pattern-spring-example-tutorial.html#ixzz4xve86pPN

阅读更多:http: //javarevisited.blogspot.com/2012/12/inversion-of-control-dependency-injection-design-pattern-spring-example-tutorial.html#ixzz4xve86pPN