Adding CDI to an existing Java EE 6 application
Join the DZone community and get the full member experience.Join For Free
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 :
- dependency injection for java (jsr 330) defines a set of annotations (@inject, @named, @provider, @qualifier, @scope and @singleton) mainly used for injection. if you download this jsr (which is actually javadoc) and read through it, you will be surprised to see that no semantic is defined (i.e. the behaviour is not portable across implementation, you could implement the injection mechanism you think is appropriate). this specification is implemented in spring 3, guice, and also in java ee 6 with cdi
- contexts and dependency injection (jsr 299) gives semantic to jsr 330 and adds more features such as context management, events, decorators, enhanced interceptors and extension mechanism.
this blog post is about adding cdi to an existing java ee 6 application .
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 kenai . it’s about crud operations on a book entity. here are the components involved :
- book : an entity describing a book (title, isbn number, price…)
- isbngenerator : a managed bean to generate random isbn numbers (you can see it as a helper class)
- itemejb : a stateless ejb to have transactions for persisting, updating, removing, finding from a database
- itemrestservice : a service that gives a restful interface to the ejb
- itemservlet : a servlet that uses the ejb to display a list of books
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 :
- @ejb is used to inject an ejb into the rest web service and the servlet
- in our example, @resource is used to inject a javax.annotation.managedbean
- isbngenerator needs to be a javax.annotation.managedbean to be injectable ( managed beans were introduces in java ee 6). if not, the container will not consider it as being a resource
- @persistencecontext injects an entity manager
- itemrestservice is annotated with @path (which is normal for a rest web service) but also with @managedbean. in fact, if a rest service wants to inject a ee resource (like an ejb) it needs to either be a @managedbean, @stateless or @singleton. in java ee 7 all the components will be managed beans, so this double annotation would not be needed any more.
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 :
- you don’t need @ejb or @resource anymore, just use @inject
- because anything is injectable with cdi, isbngenerator doesn’t need to be a managed bean but just a pojo.
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.
Opinions expressed by DZone contributors are their own.