Results 1 to 7 of 7

Thread: Sharing same bean between two servlet contexts

  1. #1
    Join Date
    Aug 2004
    Posts
    10

    Default Sharing same bean between two servlet contexts

    Hi,

    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. #2
    Join Date
    Aug 2004
    Location
    Auburn, AL, USA.
    Posts
    106

    Default

    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.
    [/list]

  3. #3
    Join Date
    Aug 2004
    Posts
    10

    Default

    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:
    http://www.fwd.at/tomcat/sharing-ses...ata-howto.html

    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. #4
    Join Date
    Aug 2004
    Location
    Auburn, AL, USA.
    Posts
    106

    Default

    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. #5
    Join Date
    Aug 2004
    Posts
    10

    Default

    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. #6
    Join Date
    Aug 2004
    Posts
    10

    Default

    Ok, got it to work.
    I basically did my own JNDI resource factory that just returns the spring bean factory.
    http://jakarta.apache.org/tomcat/tom...ces-howto.html

    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.

    Code:
    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.

    Code:
    <GlobalNamingResources>
     <Resource name="spring/beanFactory" auth="Container"
                type="org.springframework.beans.factory.xml.XmlBeanFactory"/>
    <ResourceParams name="spring/beanFactory">
        <parameter>
          <name>factory</name>
          <value>contextTest.BeanFactory</value>
        </parameter>
       
      </ResourceParams>
    
    </GlobalNamingResources>
    Code:
     <Context path="/second" docBase="../build2" debug="0" reloadable="true">
     		  <ResourceLink name="spring/beanFactory"
                global="spring/beanFactory"
              type="org.springframework.beans.factory.xml.XmlBeanFactory"/>
    
    	  </Context>

  7. #7
    Join Date
    Nov 2008
    Posts
    232

    Default

    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

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •