Java中的资源注入和依赖注入(CDI)有什么区别?

Java中的资源注入和依赖注入(CDI)有什么区别?

问题描述:

我一直在学习Java EE,发现Java EE提供了两种类型的注入机制

I have been learning Java EE for while and found Java EE provides two types of injection mechanisms


  1. 资源注入

  2. 依赖注入

请指导我理解资源注入与资源注入之间的区别。依赖注入。

Please guide me to understand the difference between Resource Injection & Dependency Injection.

Java EE提供了注入机制,使我们的对象能够获取对资源和其他依赖项的引用,而无需实例化它们直接(明确地使用'new'关键字)。我们只是声明所需的资源和我们的类中的其他依赖项通过绘制带有注释的字段或方法来表示编译器的注入点。

Java EE provides injection mechanisms that enable our objects to obtain the references to resources and other dependencies without having to instantiate them directly (explicitly with ‘new’ keyword). We simply declare the needed resources & other dependencies in our classes by drawing fields or methods with annotations that denotes the injection point to the compiler.

然后容器在运行时提供所需的实例。注入的优势在于它简化了我们的代码并将其与其依赖项的实现分离。

The container then provides the required instances at runtime. The advantage of Injection is that it simplifies our code and decouples it from the implementations of its dependencies.

这两种方法都有助于实现控制反转。

Both the methods facilitates in achieving Inversion Of Control.

看看我写的一篇文章这个。