Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 Answers

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.

like image 158
Rahul Tripathi Avatar answered Sep 12 '25 07:09

Rahul Tripathi