Miles to go …

April 19, 2010

TOTD #129: Managed Beans 1.0 in Java EE 6 – What and How ?

Filed under: General — arungupta @ 4:16 am

Two new specifications released as part of the Java EE 6 platform are Managed Beans 1.0 and Interceptors 1.1. This Tip Of The Day (TOTD) attempts to explain the basics of Managed Beans 1.0. A later blog will explain the what/how of Interceptors 1.1.

A short definition for a managed bean – its a POJO that is treated as managed component by the Java EE container.

There are several component specifications in the Java EE platform that annotates a POJO to achieve the desired functionality. For example, adding "@Stateful" annotation to a POJO makes it a stateful EJB. Similarly adding "@javax.faces.bean.ManagedBean" to a POJO makes it a JSF managed bean. Java EE 6 introduces a new specification – "Managed Beans 1.0" that provides a common foundation for the different kinds of component that exist in the Java EE platform. In addition, the specification also defines a small set of basic services:

  • Resource Injection
  • Lifecycle callbacks
  • Interceptors

The different component specifications can then add other characteristics to this managed bean. The specification even defines well known extension points to modify some aspects. For example CDI/JSR 299 relaxes the requirement to have a POJO with no-args constructor and allow constructor with more complex signatures. CDI also adds support for lifecycle scopes and events. Similarly EJB is a managed bean and adds support for transactions and other services.

A managed bean is created by adding "javax.annotation.ManagedBean" annotation as:

public class MyManagedBean {

The standard annotations "javax.annotation.PostConstruct" and "javax.annotation.PreDestroy" from the JSR 250 can be applied to any methods in the managed bean to perform any resource initialization or clean up by the managed bean. A bean with lifecycle callbacks can look like:

public class MyManagedBean {
  public void setupResources() {
    // setup your resources
    System.out.println("Setting up resources ...");
  public void cleanupResources() {
     // collect them back here
    System.out.println("Collecting them back ...");
  public String sayHello(String name) {
    return "Hello " + name;

This bean can be injected in a Servlet or any other managed component in three different ways:

  1. Using @Resource annotation as:

    @ResourceMyManagedBean bean;
  2. Using "@Inject" annotation as:
    @InjectMyManagedBean bean;
  3. Using the JNDI reference "java:app/ManagedBean/mybean" or "java:module/mybean" where "ManagedBean" is name of the deployed archive (WAR in this case), shown as:
    InitialContext ic = new InitialContext(); MyManagedBean bean = (MyManagedBean)ic.lookup("java:module/mybean");

    Its important to provide a name to the managed bean, as there is no default name, for the JNDI reference to work. EJB and CDI specifications extend this rule and provide default naming rules.

Once the bean is injected then its business methods can be invoked directly.

As part of Java EE 6, all EJB and CDI beans are defined as managed beans and so:

public class FooBean {
    . . .


public class BarBean {
    . . .

are implicitly managed beans as well.

No other beans in the Java EE platform are currently implicitly defined as managed beans. However JAX-RS resources can also be defined as EJB and CDI beans in which case the JAX-RS resources will be implicit managed beans as well. A future version of different component specifications may discuss if it makes sense to align other Java EE POJO elements to align with Managed Beans specification.

Technorati: javaee glassfish v3 managedbeans cdi ejb servlet jndi

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #134: Interceptors 1.1 in Java EE 6 – What and How ?
  2. TOTD #109: How to convert a JSF managed bean to JSR 299 bean (Web Beans) ?
  3. TOTD #144: CDI @Produces for container-managed @Resource
  4. TOTD #49: Converting a JSF 1.2 application to JSF 2.0 – @ManagedBean
  5. TOTD #94: A simple Java Server Faces 2.0 + JPA 2.0 application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3


  1. Nice overview! A quick note that JSR 313 is actually withdrawn.

    Comment by Laird Nelson — April 19, 2010 @ 12:23 pm

  2. Is there any advantage of using Managed Beans instead of CDI?

    Comment by Tetsuo — April 19, 2010 @ 12:37 pm

  3. Thanks Laird!

    Tetsuo, CDI uses the concepts defined by Managed Beans and extend them forward. All CDI beans are managed beans as well and it provides much richer set of functionality.

    Comment by Arun Gupta — April 19, 2010 @ 3:01 pm

  4. [Trackback] This post was mentioned on Twitter by arungupta: New Blog: TOTD #129: Managed Beans 1.0 in Java EE 6 – What and How ?: Two new specifications released as part of t…

    Comment by uberVU - social comments — April 19, 2010 @ 6:23 pm

  5. Hi Thanks for the explanation. Whats the difference between @Resource and @Inject ? Is there a best practice as to what to use when ? Thank you.

    Comment by codebrain — April 19, 2010 @ 9:31 pm

  6. Why jsf define a set of Annotations that existed in CDI and managedBean…
    but not reuse them and extend them

    Comment by hantsy — April 20, 2010 @ 7:54 am

  7. Tetsuo, CDI uses the concepts defined by Managed Beans and extend them forward. All CDI beans are managed beans as well and it provides much richer set of functionality.

    Comment by bagsmkt — April 23, 2010 @ 3:55 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress