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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Introduction to CDI (Part 1)

Introduction to CDI (Part 1)

In the first part of this series, we take an introductory look at Context and Dependency Injection (CDI) in Java EE and how to use producer methods.

Alex Theedom user avatar by
Alex Theedom
·
Sep. 11, 17 · Tutorial
Like (4)
Save
Tweet
Share
9.84K Views

Join the DZone community and get the full member experience.

Join For Free

Context and Dependency Injection (CDI)

Getting Started With Context and Dependency Injection

In this article, I will give an overview of some of the fundamental concepts and usages of the Context and Dependency Injection framework.

You will learn that all you need to use the dependency injection framework is a compliant POJO and a qualifying injection point and any object, including Collections, can be made injectable.

You will discover how to mark a bean so that it can be used by Expression Language to bind data directly in JavaServer Faces (JSF), and you will discover how to disambiguate a bean using the @Qualifier annotation.

You will also learn about the scoping of a bean’s lifecycle, how it matches with HTTP scopes, and viewing related scopes.

Ok, so let’s get started.

What Is CDI?

Java EE has one of the easiest-t- use and most extensible dependency injection frameworks around.

By default, almost all Java objects are injectable. All they need is to comply with JSR299 and they are instantiated by the container and are ready for injection into any qualifying injection point that the container finds.

No special annotation is required to identify the class you want to make injectable, as is often the case with other IoC frameworks. This may include EJBs, JNDI resources, Persistence Units, and Persistence Contexts, as well as any object that would have been created by a factory.

In fact, with the help of something called “producer methods,” any object can be made injectable.

Producer Methods


Producer Code

Imagine you have a list of objects, say Books, and you want to inject this into a library service. You can do that by annotating a method that returns a List of books with the annotation @Produces.

To mark an injection point, use the @Inject annotation and the instance produced will be injected. It's as simple as that.

You use the @Disposes annotation to identify the dispose parameter of a disposer method. A disposer method allows the application to perform customized cleanup of an object returned by a producer method or producer field.

The @Stereotype annotation is applied to a bean that incorporates other annotations. It is used in architecture to name reoccurring patterns. So if you find that you are using the same annotations repeatedly, you can create a stereotype that represents all those annotations.

What Next

In part two of this introduction to CDI, I discuss the use of Expression Language Beans, Type Safety, Scopes, and the extensibility of CDI.

Further Reading

Still hungry for knowledge? Want to learn more about Java EE? Then these articles will interest you:

  • JAX-RS API: an API for the construction of RESTful services.
  • Design Patterns: Professional Java EE design patterns.
  • JavaServer Faces: The JSF view language.
CDI Dependency injection Annotation

Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot, Quarkus, or Micronaut?
  • Front-End Troubleshooting Using OpenTelemetry
  • Create Spider Chart With ReactJS
  • 5 Software Developer Competencies: How To Recognize a Good Programmer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: