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

标签 java dependency-injection cdi

我学习Java EE有一段时间了,发现Java EE提供了两种注入(inject)机制

  1. 资源注入(inject)
  2. 依赖注入(inject)

请指导我理解资源注入(inject)和依赖注入(inject)之间的区别。

最佳答案

来自 source :

Resource injection enables you to inject any resource available in the JNDI namespace into any container-managed object, such as a servlet, an enterprise bean, or a managed bean. For eg, we can use resource injection to inject data sources, connectors, or any other desired resources available in the JNDI namespace.

Dependency injection enables us to turn regular Java classes into managed objects and to inject them into any other managed object (objects wich are managed by the container).

Difference between Resource Injection and Dependency Injection The differences between the RI and DI are listed below.

  1. Resource Injection can inject JNDI Resources directly whereas Dependency Injection cannot.

  2. Dependency Injection can inject Regular Classes (managed bean) directly whereas Resource Injection cannot.

  3. Resource Injection resolves by resource name whereas Dependency Injectin resolves by type.

  4. Dependency Injection is typesafe whereas Resoiurce Injection is not.

关于java - Java中的资源注入(inject)和依赖注入(inject)(CDI)有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30060876/

相关文章:

c# - 避免将具体的数据库上下文类注入(inject) Controller

java - CDI : @Resource inject in junit weld?

glassfish - 在 GlassFish 4.1 上部署 Java EE 7 应用程序时出现 java.lang.ClassNotFoundException : org. hibernate.validator.internal.cdi.interceptor.ValidationInterceptor

java - 为什么这个gson函数会保存一个空白文件?

java - 如何在 libgdx 中制作固定 Sprite (不随我的相机移动)?

java - JMenuItem-添加事件监听器

c# - IServiceCollection.AddHostedService<>();没有解决

java - 神经网络基础知识倍增

node.js - 我是否需要在 NodeJS 中进行依赖注入(inject),或者如何处理...?

java - 有没有办法在没有数据库的情况下在 Java EE 应用程序中保存数据?