What is difference between resource injection and dependency injection (CDI) in Java?

被刻印的时光 ゝ 提交于 2020-01-01 07:31:08

问题


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

  1. Resource Injection
  2. Dependency Injection

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


回答1:


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.

Take a look at one of my article I wrote on this.




回答2:


From the 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.




回答3:


Check this out: Java EE Injection

Main differences between resource injection and DI:




回答4:


Rahul Tripathi answer is exact and the correct one. But to put it in more succinct way that can help you decide which to use lets look at it this way: DI is typically used to assign implementation classes to interfaces. Whereas RI is used to extract property values, and JDBC connections from JNDI. DI allows you to write code against interfaces and decide later on which implementation to use. There is some overlap, typically when the RI'd objects are more than just a property value and when the DI is used to pass a property value. A good example of this overlap, as I already mentioned, is JDBC connections. A JDBC connection is not only a connection string (property values) it is also an implementation (which drivers to use).

I think if you are using DI and you need to pass some property value, just continue using DI. And if you are using RI and need to pass an object, than just do it in RI if possible. Ultimately if you use both, then that's more XML files that need to be filled out.



来源:https://stackoverflow.com/questions/30060876/what-is-difference-between-resource-injection-and-dependency-injection-cdi-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!