Accessing one Managed Bean from another in JSF

There is some scenarios where you need information stored in one managed beans for the business logic in some other beans.There are two ways for one managed bean to access another managed bean information.

1 . Using Dependency Injection in JSF2.0

We can use JSF 2 @ManagedProperty annotation for this purpose.In the managed bean add a @ManagedProperty annotation to the related property.

public class UserBean 

private EmployeeBean employeeBean;

public EmployeeBean getEmployeeBean()
return name;

public void setEmployeeBean(EmployeeBean employeeBean)
this.employeeBean = employeeBean;

// ....


In the above code we are accessing the EmployeeBean managed Bean property in UserBean managed class.

2 . By Using faces-config.xml


Rules :

  • The current bean must have scope which is the same as or shorter than the required bean
  • The using bean must have a property-setter method which takes the required bean as a parameter.
  • The beans cannot have managed dependencies on each other.

In Managed Bean :

FacesContext facesContext = FacesContext.getCurrentInstance();
NeededBean neededBean
    = (NeededBean)facesContext.getApplication()

Example :

Here is the example in which we are accessing method of one managed bean in another managed Bean.

public class BookBean {
    public void readBook() {
        System.out.print("I am reading a book");

public class StudentBean {
    public void readBookStudent() {
        FacesContext context = FacesContext.getCurrentInstance();
        Application application = context.getApplication();
        ValueBinding binding = application.createValueBinding("#{otherBean}");
        om.jwt.jsf.BookBean book = (com.jwt.jsf.BookBean)binding.getValue(context);



In the above code in StudentBean class we are accessing readBook() method of BookBean class:

comments powered by Disqus