Announcement Announcement Module
No announcement yet.
Spring re-creates singleton beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring re-creates singleton beans


    our application uses Spring 1.2.2 and struts. To use struts you need to define org.springframework.web.struts.ContextLoaderPlugIn as a struts plugin.

    What I found debugging through the Spring code is that Spring first creates the beans as per normal, but it creates the same beans again within ContextLoaderPlugin. Given that these beans are singletons I would have thought it would only have created them once, and re-used them again within ContextLoaderPlugIn? Given that the Hibernate session factory is so heavy it takes longer to start the application, and uses substantially more memory. Anyway, if somebody knows that re-creating the beans is normal behavour, or if there is a way to only create them once I'd like to know.

    thanks in advance.

  • #2
    How many applicationContext files do you have? i.e. do you have them seperated into different config files for DAOs, services, struts spring-managed actions, etc?

    If so, do you also have a listener configured to load other application contexts?

    It might be a matter of not loading applicationContext-*.xml if you happen to be loading all of them again... in the Struts plugin if you also have a listener.

    It might help to see your web.xml and struts-config.xml plugin snippet


    • #3

      I have 2 contexts, being applicationContext.xml and action-servlet.xml.
      Heres the web.xml:

      <web-app id="WebApp">
      <listener-class> org.springframework.web.context.ContextLoaderListe ner

      the struts-config.xml snippet is:
      <plug-in> className="org.springframework.web.struts.ContextL oaderPlugIn">
      <set-property property="contextConfigLocation"
      value="/WEB-INF/action-servlet.xml, /WEBINF/applicationContext.xml"/>

      Let me know if yo have any ideas?


      • #4
        Yeah so the ContextLoaderListener will initialize the applicationContext when the webapp is loaded, however when the ActionServlet loads its plugins, you are also passing in both applicationContext*.xmls...

        The Struts plugin / ContextLoaderListener should make the applicationContext from the webapp servlet context the "parent"..
        so you should just be able to configure the struts specific applicationContext in struts-config.xml and the other one in web.xml


        Try this... leave web.xml as is... however remove the applicationContext.xml from struts-config.xml

        <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 
            <set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml"/> 
        That should just load the applicationContext.xml once.

        I have an app using Struts, Spring, Acegi, and Hibernate... and I have about 5 applicationContext files... and I only register my applicationContext-struts.xml in struts-config.xml.

        Let me know if that helps.



        • #5

          this seems to have done the trick - and also makes sense.

          many thanks for your time!


          • #6
            This crops up time and time again, maybe it should be put in a FAQ section?

            The other common one is importing your servlet.xml into appContext.xml