1

I defined two beans for a same class with different id names in applicationContext.xml. It working perfectly, so I got confused what is exact Spring singleton scope mean. I gone through some other similar questions in Stack Overflow. But I am not clear because those not exactly same context and very long explanation.

I read that single instance for container (context). In my case, is it creating two containers for my two instances?

My code is below:

<bean id="a1" class="com.myapp.Address">
    <constructor-arg value="ghaziabad"></constructor-arg>
    <constructor-arg value="UP"></constructor-arg>
    <constructor-arg value="India"></constructor-arg>
</bean>

<bean id="a2" class="com.myapp.Address">
    <constructor-arg value="Delhi"></constructor-arg>
    <constructor-arg value="DOWN"></constructor-arg>
    <constructor-arg value="India"></constructor-arg>
</bean>


<bean id="e" class="com.myapp.Employee">
    <constructor-arg value="12" type="int"></constructor-arg>
    <constructor-arg value="Sonoo"></constructor-arg>
    <constructor-arg>
        <ref bean="a1" />
    </constructor-arg>
</bean>

<bean id="e2" class="com.myapp.Employee">
    <constructor-arg value="12" type="int"></constructor-arg>
    <constructor-arg value="Sonoo"></constructor-arg>
    <constructor-arg>
        <ref bean="a2" />
    </constructor-arg>
</bean>
halfer
  • 19,824
  • 17
  • 99
  • 186
Sun
  • 3,444
  • 7
  • 53
  • 83

4 Answers4

1

Singleton Scoped bean is straight-forward: Each bean is actually one object instantiated in the container (aka application context).

In your example, Spring container is going to create 2 Employee objects (aka Beans), which is e and e1 respectively.

Other scopes are a bit more tricky. There can be more than 1 object instantiated for a bean. For example, if you defined a bean with prototype scope, then whenever you lookup that bean from container, the container will instantiate a new object instance. For other scope it is similar: container is going to create a new instance base on specific timing (e.g. Request scope will cause a bean to create for each incoming request, etc).


Please note: Do not confuse this Singleton with the Singleton Design pattern. They have nothing in common except the "Only-one" characteristic. Singleton Design Pattern is having lots of other characteristics (e.g. a globally available lookup method; Only one instance in whole process space; Not allowed to be explicitly instantiated etc) which has nothing to do with Spring's Singleton scope

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
0

Spring singleton scope means spring container creates one object only for the same class for different instances.

For example in your code, if you do not inject values to e2 object of employee and if you try to print values for e and e2 both of the employee class, both will give the same output.

(If you are referring to a bean in the same XML file you can use <ref local=""/> instead of ref bean)

<bean id="e" class="com.myapp.Employee">
    <constructor-arg value="12" type="int"></constructor-arg>
    <constructor-arg value="Sonoo"></constructor-arg>
    <constructor-arg>
        <ref local="a1"/>
    </constructor-arg>
</bean>

<bean id="e2" class="com.myapp.Employee">
    <constructor-arg>
        <ref local ="a2" />
    </constructor-arg>
</bean>

In the above case the values for employee for both e and e2 will be 12 and Sonoo respectievely.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Anupama Rao
  • 107
  • 2
  • Ravi, It is only creating one container for both instances. In your code when you assign the value for e2, it actually changes the value for both e and e2 as it is changing the value for employee object (only 1 object is created) which is shared by both. – Anupama Rao Jul 26 '15 at 15:31
0
  • Singleton is default scope in spring i.e. one instance of bean(or object) per container.
  • Should not confuse with The Gang of Four Singleton, it is having one and only one instance per ClassLoader.
  • In your context, clearly defined two employee beans with addresses.
Premraj
  • 72,055
  • 26
  • 237
  • 180
-2

the spring singleton scope works pretty much the same as the singleton software patter (http://en.wikipedia.org/wiki/Singleton_pattern), the main idea behind this, is that you are only going to have ONE instance of each one of your declared beans in an IOC instance (spring execution context); to be more concise, every time you reference a2, e or e2 in any bean of your code using spring injection, you're going to have the SAME reference, this means, the same object in same memory location.

I recommend you take a look at this:

http://www.tutorialspoint.com/spring/spring_bean_scopes.htm and also some working examples at: http://www.mkyong.com/spring/spring-bean-scopes-examples/

One last thing, and IOC instance is different of object instance, the container instance is like you main method, so everything is executed within it, and yo create beans and do everything inside a single IOC container (off course there are some distributed environment architectures were you can have more instance of the IOC container, but I believe that's not your case)

Camilo Casadiego
  • 907
  • 3
  • 9
  • 33
  • I disagree on your view saying it works pretty much the same as Singleton Design Pattern. It is actually pretty much nothing in common, except both are trying to do is related to "singularity". However, the context of singularity, and other characteristics, are totally different – Adrian Shum Apr 17 '15 at 03:07