Providing EL Access for Beans

There are a lot of good ideas in JEE but there are also some sharp corners that the unwary developer could hurt themselves on. Over the next few articles I’m going to point out a few of these less than brilliant pieces of design so that hopefully you’ll be able to avoid them. This article is about providing Expression Language (EL) access to managed beans.

There are three annotations that, at least appear, to provide access to managed beans in JEE 6 but only one of them is worth remembering. The three annotations are:

  1. @javax.inject.Named
  2. @javax.faces.bean.ManagedBean
  3. @javax.annotation.ManagedBean

javax.injet.Named

This annotation is defined in JSR-330, Dependency Injection for Java, and is qualifier type for JSR-299, Contexts and Dependency Injection for the Java EE Platform (CDI). It provides a name for a bean making it available through the EL. This is the only annotation worth remembering and using from this set as every JEE container has CDI available.

javax.faces.bean.ManagedBean

Defined in JSR-314, JavaServer Faces 2.0, this annotation provides a method of declaring managed beans and making them accessible through the EL. This annotation allows the developer to avoid the tedium of having to declare every managed bean in the faces-config.xml file. CDI provides a more expressive and richer set of annotations for managed beans though so this can really only be seen as an alternative for when JSF 2.0 is being used in an environment where CDI isn’t present. In the real world you’d never be using JSF 2.0 in an environment that didn’t have CDI so this annotation is really surplus to requirements and can be forgotten.

javax.annotation.ManagedBean

This annotation is defined in JSR-316, Java Platform Enterprise Edition 6, and aims to generalize JSF managed beans so that they can be used elsewhere. Unfortunately there isn’t much meat on the bones so there’s not a lot you can do with the bean once you’ve created it, for example, there’s no interaction with an expression language. To compound matters CDI already does everything this does and more so there’s little point in using it. And if that wasn’t enough the final nail in the coffin would have to be the fact that if a bean meets certain criteria it will automatically be made a managed bean meaning the annotation is pointless.