Site icon Antonio's Blog

Adding CDI to an existing Java EE 6 application

In my previous post I’ve shown you how to bootstrap CDI in several environments (GlassFish, Tomcat, Jetty, Java SE). So now let’s go a bit further and use it in real code. As its name states, CDI (Contexts and Dependency Injection) is also about Dependency Injection, so let’s focus on just this feature for now. I will not define what DI (Dependency Injection) is. If you don’t know I’ll leave you to check the definition, the origins of this pattern and even a book that covers it all.

Dependency Injection was introduces in  Java EE 5. Well, to be more precise it was resource injection, i.e. you could inject container resources such as EJBs, entity managers, data sources and JMS factories and destinations into a set of defined components (Servlets, JSF backing beans and EJBs). For this purpose Java EE 5 introduced a few annotations : @Resource, @PersistenceContext, @PersistenceUnit, @EJB and @WebServiceRef. But Java EE 6 goes way beyond by introducing two specifications :

This blog post is about adding CDI to an existing Java EE 6 application.

Use case

My previous post focused on bootstrapping CDI, so I didn’t care much about the business code, that’s why I used a Hello World scenario. This time let’s make it a bit more real. I took some code from my Java EE 6 book that you can find on GitHub. It’s about CRUD operations on a Book entity. Here are the components involved :

Injection without CDI

As I said earlier, resource injection was introduced with Java EE 5 and is still available in Java EE 6. Except for the Book entity, all the components of our use case are injectable using Java EE 6 resource  injection (i.e. without CDI). The class diagram below shows you the injection annotations (@Resource, @PersistenceContext and @EJB) used to make the entire thing work.

Some things to look carefully at :

Injection with CDI

This Java EE 6 example works fine without CDI if you use resource injection. By adding CDI you will get a few enhancements that I will explain in future posts. But for now let’s just concentrate on how do you add CDI to an existing Java EE 6 application ? Very easily as I shown in my previous post, just add an empty beans.xml path to your application and this will bootstrap CDI. Then, just replace all the @Resource and @EJB with @Inject. You will get the following class diagram :

The code doesn’t change much :

Conclusion


As you can see, to add CDI to your existing Java EE 6 application you just need to add a beans.xml file, replace all @EJB and @Resource by @Inject and get rid of the @javax.annotation.ManagedBean. Well, you might think that there is not much interest in CDI if it’s only a few replace all in your code. But as you will see in my coming posts, by doing so you enter a new world : the context and dependency injection world.

Download the code, give it a try, and give me some feedback.

Exit mobile version