What is Web Beans?


This article introduces new unified framework for the Java EE environment, called Web Beans.This article is written based on the specification available at the time of writing this article. The specification for this framework is still under the public review, so there may be few changes before the final release. You can read the JSR 299 here. Web Beans is open source framework currently in the Alpha release. Gaving King, who is also the founder of famous framework JBoss Seam is also the specification lead for the Web Beans. There is lot of welcome notes for this framework and it solves many issues which is not addressed by the Java EE specifications. It is expected to be shipped with Java EE 6.0 edition. Lets jump into the next sections to read more about the Web Beans.

also read:

2.What is Web Beans?

What exactly is the Web Beans?. If you want to understand the real need for Web Beans, then little recap to the Java EE evolution is needed. The problem with the Java EE edition is, the presentation layers(Servlets, JSP and JSF) and persistance layers(EJB 3.0, JTA, JCA and JPA) are grown seperatly without much closer interactions. There is no common technology to integrate the both web tier and the persistance tier. Web Beans main goal is to address this issue by defining beans which can be interacted by multiple tiers. Web Beans will be compatiable with technologies in the both tiers. Web Beans is influenced by the popular frameworks Google Guice and JBoss Seam.

If the Java Bean has the constructor with no parameter, then it is a Web Beans. Almost every Java Bean you are writing in your code
is a Web Beans by its nature. If you have existing applications with thousands of classes, you can make use of the Web Beans specification without touching the code. This can be done by the XML configuration for all the beans.

The following are the few points to consider while writing the Web Beans:

  • To qualify as the valid Web Beans implementation, the beans must have no argument constructor. Web Beans manager calls this constructor for the initialization purpose. It is called once when the web Beans instance is created.
  • Web Beans can be implemented without no argument constructor, If it defines constructor with @Initializer annotation or any of the method with the @Initializer annotation. The methods or constructors with @Initializer will be called while bean instance is created.
  • Web Beans must be annotated with @Component. This annotation helps Web Beans manager to identify all the Web Beans in the classpath. Web Beans manager scans the classpath and load all the beans annotated with @Component.
  • From the Web Beans specification “A Web Bean implementation may be a Java class, an EJB session or singleton bean class, a producer method or a JMS queue or topic,”
  • @Initializer must be used exactly for the one constructor or one method in the class. Otherwise, DefinitionException is thrown by the Web Beans manager.

EJB 3.0 has simplified the EJB programming model and heavy use of annotations. But, even though EJB 3.0 has lot of gaps which is not addressed by the EJB 3.0 specification. One of the main problem with the EJBs are, still it cannot be used directly in the presentation layer. In the existing EJB 3.0 specification, it doesn’t provide any facilities to use EJB’s as the JSF backing beans. This gap is not filled by any of the existing technologies in the Java EE edition. The main goal of the web Beans is to enable JSF using the EJB 3.0 as the managed beans. In one word, it simplifies the interaction between application layer and the presentation layer. Unifying the two component models and enabling a considerable simplification to the programming model for web-based applications in Java. This will help the rapid application developement. Web Beans specification will be more generalized to use by any technologies in future other than EJB 3.0 and JSF.

3.Web Beans Constructor

3.1.declaration using @Initializer

Web Bean manager calls the Web Beans constructor to create the instance. The constructors for the Web Bean can be defined using
the @Initializer annotation. The same declaration as follows:

public class WebBeansSample{
	public WebBeansSample(){


In the above code, WebBeansSample is a simple Web Bean and its WebBeansSample() constructor will be invoked while creating the instance. @RequestScoped annotation specifies that the bean is visible to the other beans in the request scope. If there is only constructor in the class with no arguments, then it is not necessary to use the @Initializer annotation. @Initializer is mandatory only when if there is constructors with arguments that has to be used for the initialization purposes. Note that @Initializer can be used exactly one place in the class. That must be constructor or a method.

The following code will throw DefinitionException by the Web Bean manager:

public class WebBeansSample{
	public WebBeansSample(){

	public WebBeansSample(InjectObject obj){


the above code is not valid since @Initializer is used two times.

3.2.declaration using XML

The Web Beans constructors can be declared using the XML file. Web Beans are declared in the web-beans.xml file. The constructors
definition can be written inside the bean element. Look into the following example:


In the above code, myapp:InjectObject tells the Web Bean manager about the constructor. If the constructor definition is missing in the XML file, then it looks for the default no argument constructor in the class. When the constructor has parameters, Web Bean manager calls the method Manager.getInstanceByType() to get the value for the parameters.

5.Web Beans Lifecycle

Web Beans lifecycle depends upon the type nature of the Web Beans. Web Beans can be anything like EJBs, JMS end points or Simple Java Beans. So the lifecycle also will be different for the different type of web beans. Creating and destruction is the common lifecylcle events for all types of web beans. But, the two events behave differenetly for the different type of beans. In the next section we will look into the Creation and Destruction of the web beans.

5.1.Web Beans Creation

The create() is called when Web Beans instance is created. This method is responsible for completing the certain tasks
before the instance is created. The following are list of tasks to be performed:

  • Getting the instance for the Web Bean
  • Creating interceptors and decorator objects and binding to the web bean instance.
  • Injects all the dependencies
  • Setting the initial values defined in XML file for the Web Bean.
  • Calls the @PostConstruct method if it is required.

If there is any exception thrown while creating the instance, it will be rethrown by the create() method as the CreateException only if it is checked exception.

5.2.Web Beans Destruction

The destroy() method is responsible for destroying the web beans. The following are the tasks performed by the destroy()

  • Calls the Web Bean remove method or disposal method
  • Calls the @PreDestroy method, if required
  • Destroys all dependent objects of the instance. Web Bean manager is allowed to destroy @Dependent scoped beans at any time.


In this article you have learnt about the basic concepts behind the Web Beans specification and the main goal of the framework. Also we have explained you about the constructors and lifecycle for the Web Beans. This article doesn’t explain you about writing the example program for Web Beans. We will be publishing series of articles on the Web Beans. So, please wait we will be coming with another article on web Beans soon.

Web Beans Reference

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!