PDA

View Full Version : Problem with running multiple web applications in the same Tomcat server.


len53
May 2nd, 2008, 04:57 PM
I have the following problem while running multiple web applications using Spring MVC. I have a number of classes used by both applications and defined as “beans” in applicationContext.xml. These classes were compiled and placed into the “common” JAR file, which in turn was placed into Tomcat/shared/lib directory. This directory was referenced in the Tomcat’s conf/catalina.properties file so it will be visible by all web applications and loaded by Tomcat loader.
When I start Tomcat and all “beans” (defined in applicationContext.xml of all deployed applications) are being initialized I see (looking at log file for each application) that “init” method of the classes (from that “common” JAR) is executed in sequence as the web applications are loaded. In other words if I have appA and appB deployed in Tomcat and both have reference to Class1, then I can see in log file of appA, that “init” method of Class1 first executed using parameters specified in applicationContext.xml of appA and then is executed again using parameters specified in applicationContext.xml of appB. I have resolved ( I think ) this issue by moving the “common” JAR file from shared/lib to the each web application WEB-INF/lib folder.
But concern what I have is related to the situation when I need to define in applicationContext.xml the “beans” for classes from third-party libraries, for example the bean for JDBCTemplate, which in turn refers to the “dataSource” bean, which should be different for different web applications. In this case the classfile for JDBCTemplate is in spring.jar, which I have in Tomcat’s shared/lib. I did not test this case, but I think that appA will be based on datasource from appB, if appB will be loaded after appA.
Should I in this case also have spring.jar moved from shared/lib to each web application’s WEB-INF/lib folder.
Also does it mean that if any bean for the class (defined in applicationContext.xml for a number of web applications running in the same servlet container) should be in the JAR file placed in each web application’s WEB-INF/lib folder ( and not in the JAR located in shared folder) to avoid overriding from another web application.
All references ( that I have seen) were related to using Spring for single web application running in application server. But I did not see any publications related to running multiple web applications in the same servet container.
I believe it is very important issue and therefore ask for assistance in clarification on it.

Thank you.