Java 什么是DI容器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50718586/
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-10 23:46:58  来源:igfitidea点击:

What is a DI Container?

javadependency-injectioninversion-of-control

提问by Abdurakhmon

I am watching this course video about dependency injection and the instructor talked about di-containerbut did not explain in detail, now I read some articles and I want to confirm that now I am getting this right. Below is simple program and my question is,

我正在看这个关于依赖注入的课程视频,讲师谈到di-container但没有详细解释,现在我阅读了一些文章,我想确认现在我做对了。下面是简单的程序,我的问题是,

Is Program class below is kind of simplest di-container? if not how would simple di-container would be like

下面的 Program 类是一种最简单的双容器吗?如果不是,简单的双容器会是什么样子

interface  Implementable {
    void doSmth();
}

class A implements Implementable {

    @Override
    public void doSmth() {

    }
}

class B {

    private Implementable i;

    public B(Implementable implementable) {
        this.i= implementable;
    }

    public void doSmth(){
        i.doSmth();
    }
}

Is this class di-container ?

这个类是双容器吗?

class Program {
    B b = new B(new A());
    b.doSmth();
}

采纳答案by Steven

According to Dependency Injection Principles, Practices, and Patterns, a DI Container is:

根据依赖注入原则、实践和模式,DI 容器是:

"a software library that that provides DI functionality and allows automating many of the tasks involved in Object Composition, Interception, and Lifetime Management. DI Containersare also known as Inversion of Control (IoC) Containers." (§3.2.2)

一个软件库,它提供DI的功能,并允许自动完成许多的参与任务对象组合拦截生存期管理DI容器也称为控制反转(IoC)容器的反转。”(§3.2.2)

At the very least, a DI Container allows Auto-Wiring, which is:

至少,DI 容器允许自动接线,即:

"the ability to automatically compose an object graph from maps between Abstractionsand concrete types by making use of the types' metadata supplied by the compiler and [its runtime environment]." (§12.1.2)

通过利用编译器和[其运行时环境]提供的类型元数据,抽象和具体类型之间的映射自动组合对象图的能力。”(第 12.1.2 节)

This typically means that a DI Container will analyze a type's constructor and will inject dependencies into it, without the need of having to specify each constructor argument manually.

这通常意味着 DI 容器将分析类型的构造函数并将依赖项注入其中,而无需手动指定每个构造函数参数。

From that perspective, your Programclass is nota DI Container, but your Programclass acts as a Composer, which is part of the Composition Root. A Composition Rootis:

从这个角度来看,您的Program不是DI 容器,但您的Program类充当 Composer,它是 Composition Root 的一部分。一个 组成根是:

"a (preferably) unique location in an application where modules are composed together." (§4.1)

模块组合在一起的应用程序中(最好)唯一的位置。”(第 4.1 节)

The Composer is the part of the Composition Root that does the actual construction of the object graphs.

Composer 是 Composition Root 的一部分,它负责实际构建对象图。

"It's an important part of the Composition Root. The Composeris often a DI Container, but it can also be any method that constructs object graphs manually" (§8)

这是一个重要的组成部分组成的根。该作曲往往是一个DI容器,但它也可以是构建手动对象图的任何方法”(§8)

In your specific Composition Root, instead of using a DI Container, you are practicing Pure DI, which is:

在您特定的 Composition Root 中,您不是使用 DI Container,而是练习Pure DI,即:

"the practice of applying DI without a DI Container." ($1.1.1)

在没有 DI 容器的情况下应用 DI 的做法。”($1.1.1)

In other words, Pure DI is the practice of composing object graphs by hand using the newkeyword of your language, opposed to using a DI Container, exactly as your Programclass demonstrates.

换句话说,纯 DI 是使用new您的语言的关键字手动组合对象图的做法,而不是使用 DI 容器,正如您的Program类所展示的那样。