Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to CDI (Part 1)

DZone's Guide to

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.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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:

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
java ,dependency injection ,java ee ,cdi ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}