DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Dependency Injection in Spring
  • Singleton: 6 Ways To Write and Use in Java Programming
  • Combining gRPC With Guice
  • NullPointerException in Java: Causes and Ways to Avoid It

Trending

  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  1. DZone
  2. Coding
  3. Languages
  4. Resource Injection vs. Dependency Injection Explained!

Resource Injection vs. Dependency Injection Explained!

By 
Lalit Rao user avatar
Lalit Rao
·
Feb. 02, 15 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
68.4K Views

Join the DZone community and get the full member experience.

Join For Free

Fellow geeks, the following article provides an overview of injection in Java EE and describes the two injection mechanisms provided by the platform: Resource Injection and Dependency Injection.

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.

Note should be given for the fact that Dependency Injection is a specification (also a design pattern) and Context and Dependency Injection (CDI) is an implementation andJava standard for DI.

The following topics are discussed here:

·  Resource Injection

·  Dependency Injection

·  Difference between Context and Dependency Injection

1. Resource Injection

One of the simplification features of Java EE is the implementation of basic Resource Injection to simplify web and EJB components.

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.

The type we’ll use for the reference to the instance happen to be injected is usually an interface, which would decouple our code from the implementation of the resource.

For better understanding of the above statement let’s take a look at the example.

The resource injection can be performed in the following three ways:

·  Field Injection

·  Method Injection

·  Class injection

Now, the javax.annotation.Resource annotation is used to declare a reference to a resource. So before proceeding, let’s learn few elements of @Resource annotation.

@Resource has the following elements:

·  name: The JNDI name of the resource

·  type: The Java  type of the resource

·  authenticationType: The authentication type to use for the resource

·  shareable: Indicates whether the resource can be shared

·  mappedName: A non-portable, implementation-specific name to which the resource should be mapped

·  description: The description of the resource

Thenameelement is the JNDI name of the resource, and is optional for field- and method-based injection. For field injection, d defaultnameis the field name. For method-based injection, the defaultnameis the JavaBeans property name based on the method.

The‘name’ and ‘type’element must be specified for class injection.

Thedescriptionelement is the description of the resource (optional).

Let’s hop on to the example now.

Field Injection:

To use field-based resource injection, declare a field and annotate it with the @Resource annotation. The container will refer the name and type of the resource if the name and type elements are not specified. If you do specify the type element, it must match the field’s type declaration.

package com.example;

public class SomeClass {

  @Resource

  private javax.sql.DataSource myDB;

...

}

In the code above, the container infers the name of the resource based on the class name and the field name: com.example.SomeClass/myDB. The inferred type isjavax.sql.DataSource.class.

package com.example;

public class SomeClass {

  @Resource(name="customerDB")

  private javax.sql.DataSource myDB;

...

}

In the code above, the JNDI name is customerDB, and the inferred type is javax.sql.DataSource.class.

Method Injection:

To use method injection, declare a setter method and preceding with the @Resource annotation. The container will itself refer the name and type of the resource if in case it is not specified by programmer. The setter method must follow the JavaBeans conventions for property names: the method name must begin with set, have a void return type, and only one parameter (needless to say :P).  Anyways, if you do specify the return type, it must match the field’s type declaration.

package com.example;

public class SomeClass {

  private javax.sql.DataSource myDB;

...

  @Resource

  private void setMyDB(javax.sql.DataSource ds) {

  myDB = ds;

  }

...

}

In the code above, the container refers the name of the resource according to the class name and the field name: com.example.SomeClass/myDB. The type which is javax.sql.DataSource.class.

package com.example;

public class SomeClass {

  private javax.sql.DataSource myDB;

...

  @Resource (name="customerDB")

  private void setMyDB (javax.sql.DataSource ds) {

  myDB = ds;

  }

...

}

In the code above, the JNDI name is customerDB, and the inferred type is javax.sql.DataSource.class.

Class Injection:

To use class-based injection, decorate the class with a @Resource annotation, and set the requiredname and type elements.

@Resource(name="myMessageQueue",

  type="javax.jms.ConnectionFactory")

public class SomeMessageBean {

...

}

Declaring Multiple Resources

The @Resources annotation is used to group together multiple @Resource declarations for class injection only.

@Resources({

  @Resource(name="myMessageQueue",

  type="javax.jms.ConnectionFactory"),

  @Resource(name="myMailSession",

  type="javax.mail.Session")

})

public class SomeMessageBean {

...

}

The code above shows the @Resources annotation containing two @Resource declarations. One is a JMS (Java Messagin Service) message queue, and the other is a JavaMail session.

2.  Dependency Injection

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).

Using DI, our code can declare dependencies on any managed object. The container automatically provides instances of these dependencies at the injection points at runtime, n it also manages the lifecycle of these instances right from class loading to releasing it for Garbage Collection.

Dependency injection in Java EE defines scopes. For eg, a managed object that is only happen to respond to a single client request (such as a currency converter) has a different scope than a managed object that is needed to process multiple client requests within a session (such as a shopping cart). We can define managed objects (also called managed beans) so that we can later inject by assigning a scope to a needed class:

@javax.enterprise.context.RequestScoped  

public class CurrencyConverter { ... }

Use the javax.inject.Inject annotation to inject managed beans; for example:

public class MyServlet extends HttpServlet {

@Inject CurrencyConverter cc;

...

}

Umlike resource injection, dependency injection is typesafe because it resolves by type. To decouple our code from the implementation of the managed bean, we can reference the injected instances using an interface type and have our managed bean (regular class controlled by container) implement that interface.

I wouldn’t like to discuss more on DI or better saying CDI since we already have a great article published on this.

3. 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.


Conclusion:

Thus we learnt concept on types on Injection in Java EE and the differences between them. Just a brief. There’s more to come

Dependency injection Java EE Annotation Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Dependency Injection in Spring
  • Singleton: 6 Ways To Write and Use in Java Programming
  • Combining gRPC With Guice
  • NullPointerException in Java: Causes and Ways to Avoid It

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!