Announcement Announcement Module
No announcement yet.
How to share an ApplicationContext across multiple webapps ? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to share an ApplicationContext across multiple webapps ?


    We are currently writing an application that uses Spring for dependency injection. The application is separated into a core.jar file that contains both business layer and persistence layer services. Inside this jar file, we have a applicationContext.xml file that contains all the standard application wiring.

    The core.jar file is used by several webapps (individual WAR archives) which each includes a applicationContext.xml file that contains beans that are plugged in to the core module.

    Currently each of the webapps have a web.xml with the following entries:

      <param-value>classpath&#58;core.classpath/applicationContext.xml /WEB-INF/applicationContext.xml</param-value>
    The problem with this approach is that the applicationContext.xml from the core.jar file is not handled/loaded as a single shared ApplicationContext, which is what we would like to happen.

    My questions are as follows:
    • Is it possible to have the various webapps utilize the ApplicationContext from the core.jar file along with their own ApplicationContext in a setup where the core.jar applicationContext is shared among all webapps (As this package contains heavy weight objects) - and how can this be done ?
    • Can it be done purely declaratively or do I need to implement some custom code ?
    Thanks in advance
    Lars Bo

  • #2
    So, you'd like a kind of singleton at application server level if I'm not mistaken.
    I don't think this can be achieved with simple means. Each webapps should have a different classloader and not interact easily.
    Eventually using a jndi bound applicationContext could enable sharing of a single applicationContext across webapps.
    I'm not 100% sure about all of this interwebapp communication but I fear it works pretty much that way.



    • #3
      Hmm, I was afraid of that. I looked around the javadoc for some way of doing this, and stumbled across the SingletonBeanFactoryLocator which seems to be able to handle this. But I am not exactly sure how this can be done, and if it can be used declaratively from the wepapps or how this should be handled.

      We had the application running with the webapps sharing the same core.jar file by putting the entire solution inside tomcat's shared/lib directory and using static class variables. (I know its not nice, but it works during development - later it will be moved to a BEA Server (as an EAR file)) So I believe that it should be possible seen from a class loader perspective.

      Lars Bo


      • #4
        Yes, if you move to a j2ee app structure (EAR file) then pretty well every appserver has the option of having a shared classloader hierarchy, such that webapps are in their own classloader, but that is a child of a parent root classloader for all of them, where the service layer lives.

        Take a look at this thread for a pointer to some sample code (in the ejbtest sample in spring cvs) which shows how one webapp can trigger the loading of a shared parent context, to be used as the parent of the context for all the webapps.


        • #5
          I think the relevant thread referred to above is here.
          Last edited by Colin Sampaleanu; Nov 26th, 2005, 10:05 AM.