Results 1 to 4 of 4

Thread: Bean id's and singleton status

  1. #1
    Join Date
    Jul 2005
    Posts
    14

    Default Bean id's and singleton status

    I have a rather basic question, which after using Spring for well over a year I am surprised I have to ask, because I thought I "got it".

    Let's say I have an applicationContext which the following pertinent fragments (I've removed package names for brevity sake):

    Code:
    <bean id="serviceOne" class="defaultHandler">
    .
    .
    .
    <bean id="serviceTwo" class="defaultHandler">
    .
    .
    .
    <bean id="serviceThree" class="defaultHandler">
    .
    .
    .
    <bean id="serviceMap" class="java.util.HashMap">
        <constructor-arg>
            <map>
                <entry key="one" value-ref="serviceOne"/>
                <entry key="two" value-ref="serviceTwo"/>
                <entry key="three" value-ref="serviceThree"/>
            </map>
        </constructor-arg>
    </bean>
    My previous (to today) understanding is that even though all three of the service beans have the same class, since they have a different id Spring would instantiate 3 different instances, one for each id.

    However, I believe I am only seeing a single instance created. Currently to get around it I have added "singleton='false'" to each bean.

    Is this expected? Are singletons based on the class, not the id?

    Thanks,
    Ron

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,844

    Default

    Singleton is based on the id. Can you provide more information about why you believe you only see a single instance?

  3. #3
    Join Date
    Jul 2005
    Posts
    14

    Default

    Quote Originally Posted by Mark Fisher
    Singleton is based on the id. Can you provide more information about why you believe you only see a single instance?
    My stupid mistake. As usual, having to explain myself makes me look into things a little deeper, and it turns out I had a static in the handler that was making everything look like a single instance. Stupid.

    Thanks for the reply Mark, sorry for the noise.

    Ron

  4. #4
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,844

    Default

    No worries... glad I could indirectly help.

Posting Permissions

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