TOTD #109: How to convert a JSF managed bean to JSR 299 bean (Web Beans) ?

Guest Author

This entry is a follow up to TOTD #95 and shows how to use the recent integrations of JSR 299 in GlassFish v3 to convert a JSF managed bean to a JSR 299 bean (aka Web Beans). The TOTD #95 describes a simple Java EE 6 web application that uses Java Server Faces 2.0 components for displaying the results of a database query conducted by EJB 3.1 and JPA 2.0 classes.

The EJB class, which also acts as the JSF managed bean, looks like:

public class StateList {
EntityManagerFactory emf;
public List getStates() {
return emf.createEntityManager().createNamedQuery(”States.findAll”).getResultList();

Three changes are required to convert this class into a JSR 299 compliant bean (Web Bean) as listed below:

  1. Add an empty "beans.xml" to the WEB-INF directory.
  2. Replace "@ManagedBean" with "@javax.inject.Named annotation". "@javax.inject" annotations are defined by JSR 330.

  3. Resource injection does not work with JPA classes, yet, so populate EntityManager explicitly as explained below:

    1. Replace EntityManagerFactory resource injection:

      EntityManagerFactory emf;

      EntityManager emf = Persistence.createEntityManagerFactory("HelloEclipseLinkPU");

    2. Add the required entity classes explicitly to "persistence.xml". If the persistence unit is injected then the container automatically scans the web application root for any entity classes.
      1. Expand "Configuration Files" and edit "persistence.xml".
      2. Uncheck "Include All Entity Classes in ..." check box.
      3. Click on "Add Class...", select "state.States", and click on "OK".

That's it, re-deploy your application and now you are using the Web Beans integration in GlassFish v3 instead of JSF managed bean. The output is available at "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" as shown:

This is the exact same output as shown in TOTD #95.

Now, one-by-one, JPA, EJB, Transactions and other components will start working. Read Roger's blog for another example of Web Beans in GlassFish.

A complete archive of all the tips is available here.

Technorati: totd glassfish v3 mysql javaee6 javaserverfaces webbeans jsr299 netbeans

Join the discussion

Comments ( 4 )
  • MJ Saturday, October 3, 2009


    when I write a java prog. in IDE, for some of them 2 files with .sig & .rs are saved. what is this 2 extension? and randomely I get "error parsing file." Please help me.

  • Arun Gupta Monday, October 5, 2009

    Please post your question to nbusers@netbeans.org with more details.

  • Sendhil Chokkalingam Wednesday, November 10, 2010


    During the JavaOne conference I requested the JSR 299 reference implementation for non J2EE containers. Do you have any update on that. We are trying to use JSR 299 within tomcat environment, Please let me know


  • Arun Gupta Monday, November 15, 2010


    JSR 299 can only be used in Java EE 6 compliant app servers. However I believe Weld has an extension that allows you to use in Java SE environments as well.

    Check http://docs.jboss.org/weld/reference/latest/en-US/html/ for more details.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.