Enterprise Java without EJBs (but with CDI and MyFaces CODI)

At the W-JAX 2010 Peter Rossbach (Apache Tomcat Committer and freelancer) and I spoke about JavaEE 6 and particular the new ‘lightweight’ possibilities that are available with the WebProfile from JavaEE 6. We covered the following technologies in our talk:

  • JavaServer Faces 2.0
  • CDI
  • EJB 3.1 lite
  • JPA 2.0

We showed a demo of the integration of these tools with Apache Tomcat 7. Was good, so far….

..but why not doing it without EJB… ?

One of the above technologies is really really powerful: CDI (Contexts and Dependency Injection for the Java(tm) EE platform). With CDI it is even possible to replace EJB. All you need is an annotation (something like Spring’s @Transactional) and an Interceptor that kicks in the transaction handling for annotated methods. But… do I have to write all that stuff? The answer is NO!

The Apache MyFaces CODI project offers this as part of it’s 0.9.0 release (and way more cool CDI extensions, like new scopes etc). Don’t worry about the ‘low’ release number. It’s pretty stable but there maybe the change that there are a few changes on the API and this would be a PITA for a 1.x release…

The stack for an application like that would basically contain: JSF2, CDI, JPA. That’s it. All components are managed (including their scoping) via CDI. The JSF ‘Controller’ Bean (behind a page) uses @Inject to get access to a Service implementation:

public class SomeFacesController
@Inject private SomeService service;

Inside of the service implementation the @Inject is used in a similar way: The CDI Container injects our DAO object:

public class SomeServerImpl implements SomeService
private SomeDao dao;

The interesting part is happening here, inside of the DAO. That’s where we use the @Transactional annotation from CODI:

import org.apache.myfaces.extensions.cdi.jpa.api.Transactional;

public class SomeDao
protected EntityManager em;

public void delete(SomeObject entity)

That’s all! The stack is really really cool and there is no need for an EJB container. The Service uses a JPA-based DAO, which is uses the CODI Interceptor for the transaction handling.

I wrote two demos to use CODI + CDI with JSF (2.0 and 1.2). All you need to do to get them running is the execution of Maven (via ‘mvn’).

Have you!


Posted in apache, CDI, codi, facelets, fun, java, jetty, jsf, myfaces, myfaces codi, web², WebBeans, wjax
One comment on “Enterprise Java without EJBs (but with CDI and MyFaces CODI)
  1. Rafael Ponte says:

    Hi Mathias,

    I’ve read about CODI’s scopes and found this feature very interesting! Usually I have JSF+Spring+JPA like my main application stack but there’s a lack of useful custom scopes (well, sometimes I get this feature from Orchestra, Trinidad or even t:saveState component from Tomahawk).

    So, I would like to know if it’s possible to integrate CODI’s scopes with Spring in a useful way.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: