JSF 2 Managed Bean Injection


In this example, we will see how to inject a managed bean into the property of another managed bean using @ManagedProperty annotation.


1. Technologies used

  • JSF 2.2.8
  • Maven 3.0.5
  • Eclipse 4.4
  • JDK 1.7
  • Tomcat 7.0


2. Project structure



3. Project dependencies


4. Model Bean

File : User

We have set the eager attribute to true, which means that the user bean will be created when the application starts and not when the bean is requested for the first time.


5. Controller Bean

We will use this bean to implement the business logic of our application.

File : LoginController


We have used @ManagedProperty to inject the user bean into the property user of the loginController bean.

The dependency injection will be performed via setter method setUser(..)


6. JSF pages

File : login.xhtml


File : welcome.xhtml


7. Test It

URL : http://localhost:8080/JSF2.2-ManagedBean-Injection/

We enter a wrong password (the correct one is 111) :


Now, we enter the correct credentials walid/111 and we click the Login button:



Download source code