Thursday, May 24, 2012

JSF - Dependency Injection

Just like Spring (see this post), JSF provides support for injecting Beans into other Beans.

Let us see how this is done.

We will move the method for creating a List of Greets to a new class, then inject this class into HelloBean.

In your /src/main/java/org/confucius, create a class called GreetDAO.java, like this:

 package org.confucius;  
   
 import java.util.ArrayList;  
 import java.util.List;  
   
 import javax.faces.bean.ManagedBean;  
 import javax.faces.bean.SessionScoped;  
   
 @ManagedBean(name="greetDAO_v1")  
 @SessionScoped  
 public class GreetDAO {  
      public List<Greet> getInternationalGreetsList(){  
           List<Greet> greetList = new ArrayList<Greet>();  
             
           Greet englishGreet = new Greet("English", "Hello World!");  
           greetList.add(englishGreet);  
             
           Greet frenchGreet = new Greet("French", "Bonjour tout le Monde!");  
           greetList.add(frenchGreet);  
             
           Greet italianGreet = new Greet("Italian", "Buongiorno a Tutti!");  
           greetList.add(italianGreet);  
             
           Greet spanishGreet = new Greet("Spanish", "Hola Mundo!");  
           greetList.add(spanishGreet);  
             
           Greet swahiliGreet = new Greet("Swahili", "Jambo!");  
           greetList.add(swahiliGreet);  
             
           return greetList;  
      }  
 }  
   

Note that we have declared this class to be a Bean named "greetDAO_v1"
(Typically, DAO classes connect to a database to get data objects, but here we are create the objects on the fly - for the purpose of demo)

Next, update your HelloBean to make use of this DAO class, like this:

 package org.confucius;  
   
 import java.util.List;  
   
 import javax.faces.bean.ManagedBean;  
 import javax.faces.bean.ManagedProperty;  
 import javax.faces.bean.SessionScoped;  
   
 @ManagedBean(name="helloBean")  
 @SessionScoped  
 public class HelloBean {  
      private String greeting;  
        
      @ManagedProperty(value = "#{greetDAO_v1}")  
      private GreetDAO greetDAO;  
   
      public HelloBean(){  
           this.greeting = "Hello World JSF!";  
      }  
        
      public String getGreeting() {  
           return this.greeting;  
      }  
   
      public void setGreeting(String greeting) {  
           this.greeting = greeting;  
      }  
   
      public GreetDAO getGreetDAO() {  
           return greetDAO;  
      }  
   
      public void setGreetDAO(GreetDAO greetDAO) {  
           this.greetDAO = greetDAO;  
      }  
   
      public String getInternationalGreets() {  
           return "international_greets";  
      }  
   
      public String getAmericanGreets() {  
           return "american_greets";  
      }  
        
      public List<Greet> getInternationalGreetsList(){  
           return greetDAO.getInternationalGreetsList();  
      }  
   
 }  
   

We have injected the GreetDAO bean into this class, and are using its method to return a List of Greet objects.

If you rebuild and redeploy HelloWorldJSF, you will be able to see the data table showing languages and greets.

JSF did what is necessary to provide a GreetDAO class instance to HelloBean.

No comments: