Discussions

Web tier: servlets, JSP, Web frameworks: Unshared Object in Spring Framework

  1. Unshared Object in Spring Framework (3 messages)

    I'd like to use Spring's "dependency injections" to associate my View object with a helper object. Unfortunately, I need to construct a new helper object for each request and right now Spring's make me share one.

    I configure my view to use the helper object in the bean XML. The view is associated with the controller via the standard XmlViewResolver. The controller, view, and helper object are defined as not singleton.

    I know the object is shared because when it's invoked, it writes to an output stream that's been set. Under jmeter load testing, I find the response has already been written - the result of two views trying to share the same helper object and their output streams getting mismatched.

    Any advice would be appreciated!

    Threaded Messages (3)

  2. change the bean declaration[ Go to top ]

    in the applicationContext.xml file (or whatever alternative file name u use instead) in each bean definition u can set the singleton attribute to "false"

    <bean id="exampleBean" class="examples.exampleBean" singleton="false"/>

    now each time u refer to 'exampleBean' in the web application context it creates a new instance of that class and returns it instead (its called a 'prototype' in Spring language...although many dislike this term in this context).

    look it up in section 3.2.5 in the spring reference pdf.
  3. Not Singleton[ Go to top ]

    Sorry for any confusion, I tried to be clear the beans are marked as singleton.

    > The controller, view, and helper object are defined
    > as not singleton.

    Note that these entities are not created using an ApplicationContext or BeanFactory (which would respond as you describe), but rather by the Spring Framework when it associates the request with a controller and a view.
  4. Not Singleton[ Go to top ]

    Correction to above - "are marked as NOT singleton". Example:<bean id="InboxController" class="myco.wfds.web.gondola.InboxController" singleton="false"/>