ehardy
Sep 18th, 2004, 11:08 AM
Hi all,
We have a web application with several jar modules. Each module has it's own context. We wanted those context to be loaded automatically at application startup to form a context hierarchy. Looking at the docs, I didn't see anything that could do this out of the box. Searching through this forum, I found a couple of posts related to this same problem, among others:
http://forum.springframework.org/showthread.php?t=10228
and
http://forum.springframework.org/showthread.php?t=10335
So I started from these posts and looked at the javadocs to find out that everything was there in the Spring APIs to load the context hierarchy at application startup. All that I needed to do was to create my own ContextLoader class and a ContextLoaderListener that instantiated this custom ContextLoader. My custom ContextLoader overrides loadParentContext() to load the hierarchy, using SingletonBeanFactoryLocator.
I would like to know if this is the way it should be done? Or if anything already exists in Spring for this matter? Here is the source code. Hope it's usefull for other Spring users!
Source for context loader:
package com.pyxis.spring.web.context;
import javax.servlet.ServletContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.access.BeanFacto ryLocator;
import org.springframework.beans.factory.access.BeanFacto ryReference;
import org.springframework.beans.factory.access.Singleton BeanFactoryLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
/**
* Context loader class that loads a parent context, using
* <code>SingletonBeanFactoryLocator</code>. Uses
* <code>SingletonBeanFactoryLocator</code> default definition
* (beanRefFactory.xml) file as the default parent context definition file. It
* is possible to override this definition by adding the
* <i>parentContextConfigLocation</i> context-parame in the web.xml file.
*
* @author E. Hardy
* @version $Revision: $ $Date: $
*/
public class ContextHierarchyLoader
extends ContextLoader
{
/**
* Name of servlet context parameter that can specify the parent config
* location for the root context, falling back to the implementation's
* default else (beanRefFactory.xml).
*
* <p>This constant value is: parentContextConfigLocation
*/
public static final String PARENT_CONFIG_LOCATION_PARAM =
"parentContextConfigLocation";
/**
* Name of servlet context parameter that can specify the parent bean factory
* name for the root context, falling back to the implementation's
* default else (parentBeanFactory).
*
* <p>This constant value is: parentBeanFactoryName
*/
public static final String BEAN_FACTORY_NAME_PARAM = "parentBeanFactoryName";
private static final String DEFAULT_BEAN_FACTORY_NAME = "parentBeanFactory";
protected ApplicationContext loadParentContext(ServletContext servletContext)
throws BeansException
{
String parentContextConfig = servletContext.getInitParameter(
PARENT_CONFIG_LOCATION_PARAM);
BeanFactoryLocator locator = null;
if ((parentContextConfig != null)
&& (parentContextConfig.trim().length() > 0))
{
locator = SingletonBeanFactoryLocator.getInstance(parentCont extConfig);
}
else
{
locator = SingletonBeanFactoryLocator.getInstance();
}
String beanFactoryName = servletContext.getInitParameter(
BEAN_FACTORY_NAME_PARAM);
BeanFactoryReference bfr = null;
if ((beanFactoryName != null)
&& (beanFactoryName.trim().length() >0))
{
bfr = locator.useBeanFactory(beanFactoryName);
}
else
{
bfr = locator.useBeanFactory(DEFAULT_BEAN_FACTORY_NAME);
}
return (ApplicationContext)bfr.getFactory();
}
}
And the source for the context loader listener:
package com.pyxis.spring.web.context;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListe ner;
/**
* Bootstrap listener to start up Spring's root
* <code>WebApplicationContext</code>. This listener delegates to
* <code>ContextHierarchyLoader</code> to bootstrap the context hierarchy.
*
* @author E. Hardy
* @version $Revision: $ $Date: $
*/
public class ContextHierarchyLoaderListener
extends ContextLoaderListener
{
protected ContextLoader createContextLoader()
{
return new ContextHierarchyLoader();
}
}
Cheers,
We have a web application with several jar modules. Each module has it's own context. We wanted those context to be loaded automatically at application startup to form a context hierarchy. Looking at the docs, I didn't see anything that could do this out of the box. Searching through this forum, I found a couple of posts related to this same problem, among others:
http://forum.springframework.org/showthread.php?t=10228
and
http://forum.springframework.org/showthread.php?t=10335
So I started from these posts and looked at the javadocs to find out that everything was there in the Spring APIs to load the context hierarchy at application startup. All that I needed to do was to create my own ContextLoader class and a ContextLoaderListener that instantiated this custom ContextLoader. My custom ContextLoader overrides loadParentContext() to load the hierarchy, using SingletonBeanFactoryLocator.
I would like to know if this is the way it should be done? Or if anything already exists in Spring for this matter? Here is the source code. Hope it's usefull for other Spring users!
Source for context loader:
package com.pyxis.spring.web.context;
import javax.servlet.ServletContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.access.BeanFacto ryLocator;
import org.springframework.beans.factory.access.BeanFacto ryReference;
import org.springframework.beans.factory.access.Singleton BeanFactoryLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
/**
* Context loader class that loads a parent context, using
* <code>SingletonBeanFactoryLocator</code>. Uses
* <code>SingletonBeanFactoryLocator</code> default definition
* (beanRefFactory.xml) file as the default parent context definition file. It
* is possible to override this definition by adding the
* <i>parentContextConfigLocation</i> context-parame in the web.xml file.
*
* @author E. Hardy
* @version $Revision: $ $Date: $
*/
public class ContextHierarchyLoader
extends ContextLoader
{
/**
* Name of servlet context parameter that can specify the parent config
* location for the root context, falling back to the implementation's
* default else (beanRefFactory.xml).
*
* <p>This constant value is: parentContextConfigLocation
*/
public static final String PARENT_CONFIG_LOCATION_PARAM =
"parentContextConfigLocation";
/**
* Name of servlet context parameter that can specify the parent bean factory
* name for the root context, falling back to the implementation's
* default else (parentBeanFactory).
*
* <p>This constant value is: parentBeanFactoryName
*/
public static final String BEAN_FACTORY_NAME_PARAM = "parentBeanFactoryName";
private static final String DEFAULT_BEAN_FACTORY_NAME = "parentBeanFactory";
protected ApplicationContext loadParentContext(ServletContext servletContext)
throws BeansException
{
String parentContextConfig = servletContext.getInitParameter(
PARENT_CONFIG_LOCATION_PARAM);
BeanFactoryLocator locator = null;
if ((parentContextConfig != null)
&& (parentContextConfig.trim().length() > 0))
{
locator = SingletonBeanFactoryLocator.getInstance(parentCont extConfig);
}
else
{
locator = SingletonBeanFactoryLocator.getInstance();
}
String beanFactoryName = servletContext.getInitParameter(
BEAN_FACTORY_NAME_PARAM);
BeanFactoryReference bfr = null;
if ((beanFactoryName != null)
&& (beanFactoryName.trim().length() >0))
{
bfr = locator.useBeanFactory(beanFactoryName);
}
else
{
bfr = locator.useBeanFactory(DEFAULT_BEAN_FACTORY_NAME);
}
return (ApplicationContext)bfr.getFactory();
}
}
And the source for the context loader listener:
package com.pyxis.spring.web.context;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListe ner;
/**
* Bootstrap listener to start up Spring's root
* <code>WebApplicationContext</code>. This listener delegates to
* <code>ContextHierarchyLoader</code> to bootstrap the context hierarchy.
*
* @author E. Hardy
* @version $Revision: $ $Date: $
*/
public class ContextHierarchyLoaderListener
extends ContextLoaderListener
{
protected ContextLoader createContextLoader()
{
return new ContextHierarchyLoader();
}
}
Cheers,