Sharing spring context between web apps

Load all jars part of server classpath loading which will be used to create the spring application context and can be shared/available between Web apps/EARs.

Web.xml (in all WEB apps)

<context-param>
  <param-name>parentContextKey</param-name>
  <param-value>beanRefFactory</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 It looks for beanRefContext.xml part of classpath and a spring bean named “beanRefFactory”

beanRefContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 <bean id="beanRefFactory"
  class="org.springframework.context.support.ClassPathXmlApplicationContext">
  <constructor-arg >
    <list>
      <value>classpath*:spring-context.xml</value>
   </list> 
  </constructor-arg>
 </bean>
</beans>

spring-context.xml contains spring beans definition needed by the application and been loaded to create spring application context. This will be the parent application context.

Ensure that applicationContext.xml is placed in WEB-INF folder, used to create its own spring context which extends the parent application context created already. Define the spring beans specific to web modules here. if not, have dummy declaration given below.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
</beans>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: