Announcement Announcement Module
No announcement yet.
Sharing same bean between two servlet contexts Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sharing same bean between two servlet contexts


    I was wondering how it would be possible to share the same bean-instance (and preferably the whole spring-context) between two different servlet contexts?

    It would be usefull eg. when your bean has a cache and both servlet contexts need to use this.

    Could I somehow register the bean factory in a separate context and somehow pick it up from there (now I just use the normal listener approach for setting up the factory).
    I mean using eg. ServletContext.getContext("Spring").

    Any ideas?

  • #2
    This is one area where there isn't any support from the standards. Unfortuantly the Servlet Spec. assumes all your web apps will either be complete unto themselves, or make use of backend servers (EJB).

    My understanding of your options is:
    1. Use an EJB server.
    2. Use some other type of JCP approved backend server - JMX, etc...
    3. Use a custom web services backend (burlap, etc...)
    4. Hack something together based on your knowledge your servlet containers classloading.


    • #3
      Yeah, I have tought about some of these options myself.

      But I had an idea to use a "self made" servlet context and store the spring bean factory there. But I don't know yet if the factory will be the same instance for the other two servlet contexts.

      Eg. I have contexts "A" and "B" (defined in server.xml), and store the spring factory in "C", which I can access using ServletContext.getContext("C").
      I haven't yet tried this as I'm new to Spring and haven't made the needed spring factory initialization code. Should be easy though.

      Here's a link to "shared contexts" in tomcat:

      I would like to avoid using JMS or other solutions for syncing state between the contexts, as they are running in the same JVM (and this would add unneeded complexity). It would just be easier to get the same singleton for both contexts.

      We'll see...


      • #4
        Oh yeah, one other option is that Tomcat has a (simple) built in JNDI Context, you should be able to use this to store references to your bean(s). I think you would have to put the JAR with your bean in it in the common/lib directory or something like that. Note that this does tie you to Tomcat, but if you are confident that you won't be switching this might be the best solution for you. If you get it to work I'd like to hear how it goes.


        • #5
          Thanks, good idea, hadn't tought of that myself.
          I'm actually using tomcat (in the future as well) so that's not a problem.
          I'll tell you how it went if I get it to work.


          • #6
            Ok, got it to work.
            I basically did my own JNDI resource factory that just returns the spring bean factory.

            Only caveat was that you can't have spring.jar in your WEB-INF/lib and common/lib or you'll get ClassCastExceptions (the same may apply for your resource factory class and spring beans, I had them only in common/classes).
            Will have to see how to solve this if I need spring directly in my webapp.

            public class BeanFactory implements ObjectFactory {
              private static XmlBeanFactory bf;
              static {
                ClassPathResource res = new ClassPathResource("applicationContext.xml");
                bf = new XmlBeanFactory(res);
              public Object getObjectInstance(Object obj,
                  Name name, Context nameCtx, Hashtable environment)
                  throws NamingException {
                return bf;
            The following is in tomcat's server.xml in the appropriate locations.

             <Resource name="spring/beanFactory" auth="Container"
            <ResourceParams name="spring/beanFactory">
             <Context path="/second" docBase="../build2" debug="0" reloadable="true">
             		  <ResourceLink name="spring/beanFactory"


            • #7
              I am facing a similar problem in tomcat 6
              I hav a jar which has some java beans which i need to access in two different web apps

              i did same as above but the tomcat is not starting

              I am confused where to put the factory class jar and the spring jar do i put them in bootstrap classpath