Magazine
 
Integrating JSF, Spring and Hibernate
 

Configuring WebApplicationContext

First of all it is necessary to configure the WebApplicationContext as a ContextListener in the web.xml file of the web application.

<listener>
<listener-class>
org.springframework.web.context.
ContextLoaderListener
</listener-class>
</listener>

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:

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.
context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

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

<context-param>
<param-name>contextConfigLocation</
param-name>
<param-value>/WEB-INF/
applicationContext-hibernate.xml</paramvalue>
</context-param>

 

 

ContextLoaderListener loads the configuration parameters from applicationContexthibernate. 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 = WebApplicationContextUtils. getWebApplicationContext
(servletContext);

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 (ServiceFinder.java):

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
org.springframework.web.context.support.
WebApplicationContextUtils;
import java.util.Map;
import javax.servlet.ServletRequest;
import
javax.servlet.http.HttpServletRequest;
public class ServiceFinder {

Jan 2008 | Java Jazz Up | 71
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 ,

83, 84 , 85 , 86, 87 , 88, 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103, 104 , 105 ,

106, 107,

Download PDF