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

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

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:

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

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

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}