This is quite easy using the built in optional loading of a parent context to the main web app context, that the ContextLoader can do.
Code:
web.xml:
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext1.xml
</param-value>
</context-param>
<!-- load a shared service layer parent application context -->
<context-param>
<param-name>locatorFactorySelector</param-name>
<param-value>beanRefContext.xml</param-value>
</context-param>
<context-param>
<param-name>parentContextKey</param-name>
<param-value>servicelayer-context</param-value>
</context-param>
and in beanRefContext.xml, somewhere on your classpath:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: beanRefContext.xml,v 1.1 2004/11/14 17:19:19 colins Exp $ -->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- load a hierarchy of contexts, although there is just one here -->
<beans>
<bean id="servicelayer-context"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>/WEB-INF/applicationContext2.xml</value>
</list>
</constructor-arg>
</bean>
</beans>
The loading of the shared parent context happens via ContextSingletonBeanFactoryLocator. Take a look at the JavaDocs for that to get an idea of what is going on.
Note this is a _shared_ parent context. Any other webapps in the same EAR file (if uisng an EAR file) that specify the same beanRefContext will share the same parent context, and the same woiuld happen with any other utility code that did the same thing...
Regards,