What is exact meaning of Spring Singleton scope?

隐身守侯 提交于 2019-12-11 10:40:10

问题


I defined two beans for a same class with different id names in applicationContext.xml. It working perfectly, So I got confuse what is exact spring singleton scope mean. My code is below. I gone through some other similar questions in stackoverflow. But I am not clear because those not exactly same context and very long explanation.

Please share me if you know what it is

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

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

回答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




回答2:


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.




回答3:


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



回答4:


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)



来源:https://stackoverflow.com/questions/29688657/what-is-exact-meaning-of-spring-singleton-scope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!