Skip to main content

WebBeans in Glassfish v3

Posted by cayhorstmann on July 5, 2009 at 9:39 PM PDT

As I am updating my share of chapters in the Core JavaServer Faces book
(with the hard parts fortunately being tackled by my coauthor, David Geary), I
started playing with WebBeans, erm, Java Contexts
and Dependency Injection.
I'll keep calling it WebBeans—the alternative JavaCandi is more than I
can take. There are two features that are crucial for JSF users: Conversation
scope and access to stateful session beans from JSF pages. (Seam users know all
about this and can skip this blog.)

I have been using Glassfish for most of my test programs, mostly because I
am used to it. But today, as I did some experiments with JBoss, I found another
reason to like Glassfish—it boots a lot faster. (I remember a
rather dismal Java One session with Jerome Dochez a couple of years ago where
he demoed the embryonic Glassfish v3 and how it was wonderfully modular, and I
thought “Oh boy, href="">Second System
Syndrome”. Well, I was wrong, and Glassfish v3 is wonderfully fast.)

The current Glassfish v3 Preview has some support for WebBeans. As of today,
it contains a 2-month old version of WebBeans and a more recent WebBeans
integration module (look for Glassfish JCDI in the update tool).

I got my first trivial app to work after paying attention to these

  • Import javax.annotation.Named and
    javax.context.SessionScoped—that's where the classes
    were way back when
  • Be sure to add an empty WEB-INF/beans.xml
  • Make your bean serializable

Then simply replace @ManagedBean @SessionScoped with
@Named @SessionScoped, and you are good to go.

Of course, that's just another way of achieving what you can already do with
JSF 2.0. For something strictly better, turn to conversation scope. In JSF, you
have the uncomfortable choice between request scope (which is too short) and
session scope (which is too long). JSF 2.0 gives us view scope and a flash
object, neither of which is all that helpful in general. Conversation scope is
what you want. You turn it on when you want to start a sequence of interactions
with the user, then turn it off when that interaction has finished. The user
can open two browser tabs with different conversations.


It's easy with WebBeans, and it works with the current Glassfish v3. Change
@SessionScoped to @ConversationScoped. Add a field

private @Current Conversation conversation;

and calls




into suitable action methods. The life of the bean is extended from request
to conversation scope when you call begin, and it ends when you
call end.

Perhaps this should have been a feature of JSF 2.0, but I take it any way I
can get it.

The other key feature is the ability to skip managed beans altogether and to
invoke stateful session beans from your JSF pages. This does not seem to work
in the current Glassfish, which is too bad. I get a warning

INFO: Web Beans 1.0.0.PREVIEW1
INFO: Transactional services not available. Transactional observers will be invoked synchronously.
INFO: EJB services not available. Session beans will be simple beans, injection into non-contextual EJBs, injection of @EJB in simple beans, injection of Java EE resources and JMS resources will not be available.
INFO: JPA services not available. Injection of @PersistenceContext will not occur. Entity beans will be discovered as simple beans.
INFO: @Resource injection not available.

That's when I installed JBoss 5.1.0GA. Here are a few things I learned:

  • JBoss 5.1.0 includes a more modern version of WebBeans than Glassfish
  • WebBeans support seems pretty complete, much more than in Glassfish
  • JBoss startup is s l o w compared to Glassfish
  • Hot deployment is lukewarm in JBoss. I had to restart the server more
    often than with Glassfish
  • The logs are at server/default/log/server.log, and they
    don't show up as nicely in Eclipse as the Glassfish logs
  • You can use JSF 2.0 with JBoss 5.x simply by replacing the jsf-api.jar
    and jsf-impl.jar in the
    server/default/deploy/jbossweb.sar/jsf-libs directory with the
    Mojarra 2.0 beta
  • The Eclipse adapter for JBoss 5.0 almost works with 5.1, but it doesn't
    import the JSR299 API. I had to manually add the JAR file to the project.
    has a remedy.


Whilst 299 only specifies an EE environment, Web Beans will work in SE (we have a simple example using Swing), and can degrade gracefully from full EE (with transactional services, EJB support, JPA support etc.) down to Servlet (Tomcat) only and so on. You see those messaages because, as Roger says, Web Beans in GlassFish doesn't currently support EE services, only Servlet. As you correctly identify, JBoss AS does support EE services for Web Beans. I know the GlassFish guys are working hard on adding the EE support :-) BTW I've not had any problems with JBoss 5.1 and the Eclipse support, but I use JBoss Tools 3.1 M2 ( which bundles an update IIRC to allow JBoss 5.1 support.

Hello Cay - We have not implemented Web Beans EJB support in GlassFish yet. We are working on that. Glad to hear that you were able to use the non EJB portion.