Java中的资源注入和依赖注入(CDI)有什么区别? [英] What is difference between resource injection and dependency injection (CDI) in Java?

查看:187
本文介绍了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.

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

这篇关于Java中的资源注入和依赖注入(CDI)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆