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.
Join the DZone community and get the full member experience.Join For Free
FlexNet Code Aware, a free scan tool for developers. Scan Java, NuGet, and NPM packages for open source security and open source license compliance issues.
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.
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.
In part two of this introduction to CDI, I discuss the use of Expression Language Beans, Type Safety, Scopes, and the extensibility of CDI.
Still hungry for knowledge? Want to learn more about Java EE? Then these articles will interest you:
Published at DZone with permission of Alex Theedom , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.