When it comes to creating an application with Java (now Jakarta) EE, the first specification you usually get in touch with is CDI (Contexts and Dependency Injection). Starting with this specification, you'll probably know it for its dependency injection (DI) capabilities:
@Inject. Even besides DI, the CDI spec offers a lot more: events, decorators, interceptors, etc. Well-known books about Java EE (find some of them here) sometimes only cover basic CDI functionality and rarely dedicated more than one chapter for it. Fortunately, Jan Beernink and Arjan Tijms recently published a book to cover the CDI spec in-depth: Pro CDI 2 in Java EE 8 – An In-Depth Guide to Context and Dependency Injection.
The whole book consists of 241 pages and is available both as a digital and print version (e.g. on Amazon). I got the PDF version from Arjan and was able to read it in four days while trying out some examples in the IDE in parallel. My CDI knowledge before reading the book was more basic. I was able to use the concepts, but never touched advanced topics and still had some knowledge gaps to connect all the dots. This changed a lot after reading the book!
The history of CDI
The book starts with a throwback of important people and events in the history of the CDI specification. This was extremely valuable for me, as I wasn't aware of how everything started and who drove the success of CDI (btw. I am 24 and started with Java EE 7). You'll learn about how Rod Johnson, Gavin King, and Bob Lee had an influence on CDI. Furthermore, this chapter includes the evolution of the major Java EE specifications with an own dependency injection framework (e.g. JSF, JAX-RS…) alongside CDI and you'll realize that CDI is a rather young spec, compared to EJB or JPA.
Next, the authors describe the influence of proprietary DI frameworks in the past for CDI and how the AtInject JSR is related to CDI. This chapter is a nice recap of how the specification evolved to a more or less platform DI framework for Java EE, as more and more specifications plan to remove their own injection framework.
CDI beans, scopes, and qualifiers
In the following chapters of the Pro CDI 2 book, you get a detailed introduction of what the component model and a CDI bean actually is. This includes a lot of basic knowledge, you need to understand CDI in full execution. You'll learn about the different ways to identify and retrieve a CDI bean. Next, the authors present all different scopes and when to use them. In addition, they provide two good examples to write your own CDI scope and how to integrate it with a CDI extension.
All of this was really important for me to connect the missing dots, as I always mixed up what
@Named etc. actually does.
CDI events in the Pro CDI 2 book
Another important aspect of the CDI specification is events. With CDI it's really easy to emit and observe events to decouple parts of your application. The authors dedicate an own chapter to this topic and explain it in detail. You'll also get to know how to apply CDI qualifiers to your events to distinguish them. Both synchronous and asynchronous events are covered. For the asynchronous part, you'll also learn how you can efficiently use the returning
CompletionStage<T> to further process or handle errors.
Decorators and interceptors with CDI 2
Next, decorators and interceptors are covered in the Pro CDI 2 book. Even though the interceptor part originates from an own spec, they have a tight integration to CDI. For both of these concepts, the authors provide good examples when to use them and what the main differences are. Aside from creating them, you'll also learn the ways to activate them and how to put them in order.
For an introduction to CDI interceptors, you can have a look at one of my previous blog post.
Dynamic beans and CDI 2 in Java SE/FX
One of the major advanced CDI topics is Dynamic Beans. The book has an own chapter for covering this and you get examples for both implementing the
Bean<T> interface and using a CDI extension alongside a configurator. The authors also emphasize that Dynamic CDI Beans might rarely be a requirement for usual business applications. In contrast, they also provide good examples where it could make sense.
The final chapter of the books gives you an introduction on how to bootstrap CDI in a Java SE and also JavaFX (replacing JavaFX injection) environment. All CDI implementations (e.g. Weld or Apache OpenWebBeans) supported an API to bootstrap the CDI container in a plain Java SE app for a long time. However, this manual bootstrap API was not standardized until CDI 2. You'll learn how to include it to your project, scan for beans, and also the differences compared to a Java EE environment. This section also covers how to use this approach for unit testing with JUnit 5.
To summarize it, I can definitely recommend you to buy and read this book. The authors did great work to produce such an in-depth guide. After reading this book you'll definitely know everything to effectively use CDI. They balance theoretical, practical, and historical explanations in a remarkable way. Their examples always include real-world business problems and are not artificial (like foo & bar). Not only is CDI one of the most central specifications in Java (now Jakarta) EE but also the right use of it makes your development life easier.
Feel free to add a comment if you plan to read it or have something to add.
PS: If you are looking for a reference for basic CDI knowledge, take a look at this introduction to CDI.
Have fun reading this excellent book about CDI 2,