JSF Managed Beans

Managed Beans:

Managed bean is a bean that is being accessed from Jsf Page. It can be a normal bean with getter setter,business logic or a backing bean. You never have to manually call new( ) on a managed bean. Rather, managed beans are "lazily initialized" by the container at runtime, only when needed by the application.

Managed Bean with xml configuration: Here bean is specified in faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
    <managed-bean>
	  <managed-bean-name>sampleBean</managed-bean-name>
	  <managed-bean-class>com.javasafari.SampleBean
	                      </managed-bean-class>
	  <managed-bean-scope>request</managed-bean-scope>
	  <managed-property>
	  <property-name>name</property-name>
	  <value>Ankit</value>
	  </managed-property>
     </managed-bean>
</faces-config>

Managed Bean with Annotation:

package com.javasafari;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
 
@ManagedBean(name=sampleBean)
@RequestScoped
public class SampleBean implements Serializable {
 
	private static final long serialVersionUID = 1L;
	
	@ManagedProperty(value="Ankit")
	private String name;
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
}

With name attribute another important attribute is eager. If eager="true" then managed bean is created before it is requested for the first time else "lazy" initialization is used in which bean will be created only when it is requested.

Initializing List Property of a bean:

<managed-property>
  <property-name>friendsName</property-name>
	<list-entries>
	  <value-class>java.lang.String</value-class>
	  <value>Ankit</value>
	  <value>Ajay</value>
	  <value>Piyush</value>
	  <value>Swati</value>
  </list-entries>
</managed-property>

Initializing Map property of a Bean:

<managed-property>
  <property-name>friends</property-name>
	<map-entries>
	<map-entry>
	  <key>Ankit</key>
	  <value>26</value>
	</map-entry>
	<map-entry>
	  <key>Ajay</key>
	  <value>26</value>
	</map-entry>
  </map-entries>
</managed-property>

Declaring Lists and Maps Directly as Managed Beans: We can define list and maps as managed beans.

<managed-bean>
  <managed-bean-name>empNames</managed-bean-name>
  <managed-bean-class>java.util.ArrayList</managed-bean-class>
  <managed-bean-scope>none</managed-bean-scope>
  <list-entries>
	<value>Ankit</value>
	<value>Ajay</value>
	<value>Piyush</value>
  </list-entries>
</managed-bean>

Managed Bean Interdependence: It is basically Inversion Of Control.

Suppose we have addressBean

<managed-bean>
  <managed-bean-name>addBean</managed-bean-name>
  <managed-bean-class>com.javasafari.Address
</managed-bean-class>
<managed-bean-scope>none</managed-bean-scope>
</managed-bean>

Now we are having one Employee bean which is having one property of type Address Bean, then we can do the mapping as shown below:

<managed-bean>
  <managed-bean-name>empBean</managed-bean-name>
  <managed-bean-class>com.javasafari.EmployeeBean
                          </managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
<managed-property>
  <property-name>firstName</property-name>
</managed-property>
<managed-property>
<property-name>homeAddress</property-name>
  <value>#{addBean}</value>
</managed-property>
</managed-bean>

Here homeAddres property is having value as object of Address Bean.

Using Annotation we can do the same as:

@ManaqedProperty(value="#{addBean}")
private Address homeAddress;

Scopes of Managed Beans:

ScopeDescription
@RequestScoped Managed beans registered with request scope will be instantiated and stay available throughout a single HTTP request. This means that the bean can survive a navigation to another page, provided it was during the same HTTP request.
@NoneScoped Managed beans with a none scope are not instantiated nor stored in any other scope. Instead, they are instantiated on demand by another managed bean. Once created, they will persist as long as the calling bean stays alive because their scope will match the calling bean's scope.
@ViewScoped Managed beans with view scope lives as long as user is interacting with the same JSF view in the browser window/tab. It get created upon a HTTP request and get destroyed once user postback to a different view.
@SessionScoped Managed beans with session scope stays as long as the HTTP session lives. It get created upon the first HTTP request involving this bean in the session and get destroyed when the HTTP session is invalidated.
@ApplicationScoped Managed beans registered with an application scope retain their values throughout the lifetime of the application and are available to all users.
@CustomScoped If the content of the element, or the value of the value attribute on the @CustomScoped annotation is an EL Expression, and the expression evaluates to a class that implements java.util.Map, then the runtime uses the map itself as the scope into which the instance of the managed bean will be placed. Bean lives as long as the bean's entry in the custom Map.