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

  • singleton

    Suppose i declare a bean in my conf. file as singleton="false"

    And in another bean i pass this bean as dependency through setter..

    Then for this bean ...wont the earlier bean be acting as a singleton.....

  • #2
    No. Spring will create a proxy for al non-singleton beans and for each invocation retrieve the correct instance.


    • #3
      Maybe it's Monday morning and I'm really tired, but I thought that would be the case as well. Although it's a prototype, getBean(..) will be called once when the dependency is injected. Therefore that dependency will always be used for every invocation of the owning singleton. Unless you marked it as a scoped-proxy. Feel free to correct me if the weekend is still lingering in my brain.


      • #4
        When in doubt, consult the javadocs
        	 * Set if this a <b>Singleton</b>, with a single, shared instance returned
        	 * on all calls. In case of "false", the BeanFactory will apply the <b>Prototype</b>
        	 * design pattern, with each caller requesting an instance getting an independent
        	 * instance. How this is exactly defined will depend on the BeanFactory.
        	 * <p>"Singletons" are the commoner type, so the default is "true".
        	 * Note that as of Spring 2.0, this flag is just an alternative way to
        	 * specify scope="singleton" or scope="prototype".
        	 * @see #setScope
        	 * @see #SCOPE_SINGLETON
        	 * @see #SCOPE_PROTOTYPE
        From the javadocs...

        It is a prototype so every call to getBean would create a new instance. The method getBean is indeed called on creation and that is the only time it is being called. Defining another scope as prototype (i.e. Request or Session) would change that behavior.

        So Karl you were right, weekend isn't lingering in your brain, at least not at this occasion .


        • #5
          Originally posted by mdeinum View Post
          So Karl you were right, weekend isn't lingering in your brain, at least not at this occasion .
          That's good to hear, thanks for the clean bill of health .