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

  • Spring re-creates singleton beans

    Hi,

    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?

    Code:
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    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

    Comment


    • #3
      Hi,

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

      <web-app id="WebApp">
      <display-name>fullcourtweb</display-name>
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
      </context-param>
      <listener>
      <listener-class> org.springframework.web.context.ContextLoaderListe ner
      </listener-class>
      </listener>


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


      Let me know if yo have any ideas?

      Comment


      • #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

        i.e.

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

        Code:
        <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 
            <set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml"/> 
        </plug-in>
        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.

        Cheers
        Mark

        Comment


        • #5
          Mark,

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


          many thanks for your time!

          Comment


          • #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

            Comment

            Working...
            X