Managed Beans configuration In JSF 2.0 | Techartifact

In JSF 2.0, Java bean that is accessed from JSF page is called a Managed Bean. The managed bean can be a normal Java bean, which contains the getter and setter methods, business logic or even a backing bean (a bean contains all the HTML form value).

There are two ways to configure the managed bean :

-> Configure Managed Bean with faces-config.xml

It is same as we used to do JSF 1.1 .We can defined managed bean in faces-config.xml .

<?xml version="1.0" encoding="UTF-8"?>

Using Dependency Injection

-> Configure Managed Bean with Annotation

You can annotated a Managed Bean with new @ManagedBean annotation in JSF 2.0 .JSF is a basic Dependency Injection (DI) container and we can use annotations to inject objects. JSF offers setter method injection – this means the object will be passed into the setter. It is also static injection – meaning, the injection will happen only during bean creation (as opposed to Seam, where static and dynamic injection is possible).

package com.vinay.common;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

public class HelloBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private String name;
	public String getName() {
		return name;
	public void setName(String name) { = name;

@ManagedBean – marks this bean to be a managed bean with the name specified in name attribute. If the name attribute in @ManagedBean is not specified, then the managed bean name will default to class name portion of the fully qualified class name.Try to set the scope (request) into which this bean will be placed. If scope is not specified then bean will default to request scope.

@ManagedBean also has eager attribute (new in JSF 2). If eager=”true” and scope is application, then this bean must be created when the application starts and not during the first reference to the bean. In other words, the creation and storing of the bean instance must happen before any requests are serviced.

@ManagedBean(name="vinayBean", eager=true)
public class Vinay{

If eager is true but scope is not application, then regular “lazy” initialization used. If eager is not defined or missing, then “lazy” initialization is used as well.

You can also pass the value of variable using annotation.

private String name;

When the bean is created, ‘vinay’ will be passed to setName(..) method.

Happy New year guys.This is first post of this year.
Happy coding with Techartifact.


I am an Oracle ACE in Oracle ADF/Webcenter. Sr Java Consultant-working on Java/J2EE/Oracle ADF/Webcenter Portal/ content and Hibernate for several years. I'm an active member of the OTN JDeveloper/Webcenter forum. Passionate about learning new technologies. I am here to share my knowledge. Give your views and suggestion on [email protected] .

More Posts - Website

Follow Me:
TwitterLinkedInGoogle PlusYouTube