问题
I have been learning Java EE for while and found Java EE provides two types of injection mechanisms
- Resource Injection
- 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.
Resource Injection can inject JNDI Resources directly whereas Dependency Injection cannot.
Dependency Injection can inject Regular Classes (managed bean) directly whereas Resource Injection cannot.
Resource Injection resolves by resource name whereas Dependency Injectin resolves by type.
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