How applications share spring container instead of
creating a separate copy for each other.
Let’s consider following example:
A web application (mywebapp.war) created to provide business services for the general clients.
This application uses spring container to manage the beans and transactions.
In the same web application we have an axis based web service application.
This axis application also needs to have a spring container.
Now we'll look at how code can be modified to share the spring container/context.
In mywebapp.war, we’ll look at the web.xml
<context-param>
<param-name>locatorFactorySelector</param-name>
<param-value>classpath:beanRefFactory.xml</param-value>
</context-param>
<context-param>
<param-name>parentContextKey</param-name>
<param-value> myShareSpringContext </param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-config-empty.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
‘locatorFactorySelector’ will be pointing to the xml file which contains the
spring bean configuration file names. In our example file name is beanRefFactory.xml.
If we look at the contents of this file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="myShareSpringContext"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>spring-config.xml</value>
</list>
</constructor-arg>
</bean>
</beans>
For the ‘parentContextKey’ parameter, we have assigned ‘myShareSpringContext’.
For the ‘contextConfigLocation’ parameter we specify a empty spring
configuration xml file. In our example the file name is ‘spring-config-empty.xml’.
Because we will be using a common spring configuration file in a different location.
This spring-config-empty.xml will have the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans></beans>
In web application we are loading the spring context from the following
listener ‘org.springframework.web.context.ContextLoaderListener’.
If we analysis this code, it’s designed to load the new context or use an existing context.
Simply share the context.
Now if we look at the axis web service implementation,
xxxSoapBindingImpl.java:
String locatorFactorySelector = "classpath:beanRefFactory.xml";
String parentContextKey = " myShareSpringContext ";
BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(locatorFactorySelector);
BeanFactoryReference parentContextRef = locator.useBeanFactory(parentContextKey);
context = (ApplicationContext) parentContextRef.getFactory();
Now from the ‘context’ object reference we can retrieve the spring managed beans.
We have used ‘ContextSingletonBeanFactoryLocator’ to retrieve the
spring container location. If we analysis this class,
it has its own hashmap to store the applicationcontext. Key will be the parentContextKey.
Now both web application and soap impl will use the same spring container.
For more information please refer following links:
1] SingletonBeanFactoryLocator & ContextSingletonBeanFactoryLocator,
class documentations from spring framework documentation
2] http://slawek-zachcial.blogspot.com/2005/03/mdbstrutsspringhibernate-weblogic-way.html
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment