Integrating JSF, Spring and Hibernate

In this section we will explain you the process of Integrating Spring with JSF (Java Server Faces) technology.

Integrating JSF, Spring and Hibernate


In this section we will explain you the process of Integrating Spring with JSF (Java Server Faces) technology. This section gives you a brief description about Spring container (a WebApplicationContext), which contains all the 'business beans' present in the application.

Configuring Spring context(WebApplicationContext)

What is WebApplicationContext?

The WebApplicationContext is an interface that extends the ApplicationContext interface in the Spring framework. This interface is used to provide the configuration for a web application. The WebApplicationContext is ready only while application is running, it can even be reloaded in runtime if the implementation supports this.

Configuring WebApplicationContext

First of all it is necessary to configure the WebApplicationContext as a ContextListener in the web.xml file of the web application. Following code shows the configuration:


In case you are using an older version of the Servlet API (<2.3), you have to use Spring's ContextLoaderServlet in order to configure WebApplicationContext. You can use the following code in the web.xml file:


The ContextLoaderListener uses the configuration defined in applicationContext-hibernate.xml file and creates the object of WebApplicationContext for the application. The path of applicationContext-hibernate.xml is passed as <context-param>..</context-param> property. Following code can be used for this purpose:


When the application is started, ContextLoaderListener loads the configuration parameters from applicationContext-hibernate.xml file and creates an object of WebApplicationContext and stores it the ServletContext of the web application.

Now our application can use the WebApplicationContext to find the beans present in it.

Getting the reference of Application context:

ApplicationContext appContext =

Getting the bean from the ApplicationContext 

Object o =appContext.getBean(beanName);

Developing Service Finder

In our application we will use Service Finder class to find the beans managed by Spring framework. Here is the code of the Service finder utility(

package  net.roseindia.web.common;
import javax.faces.context.FacesContext;
import javax.faces.context.ExternalContext;

import javax.servlet.ServletContext;

 import org.springframework.context.ApplicationContext; 
 import java.util.Map; 
 import javax.servlet.ServletRequest; 
 import javax.servlet.http.HttpServletRequest; 
 public class ServiceFinder { 

  public static Object findBean(String beanName){
  FacesContext context= FacesContext.getCurrentInstance();

  ServletContext servletContext = 
  ApplicationContext appContext =

  Object o =appContext.getBean(beanName);

  return o;



The findBean() method of ServiceFinder class is used to get the reference of in the backing beans of JSF. 

Share on Google+Share on Google+

Integrating JSF, Spring and Hibernate

Posted on: December 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Integrating JSF, Spring and Hibernate   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image