Spring Singleton范围的确切含义是什么? [英] What is exact meaning of Spring Singleton scope?

查看:105
本文介绍了Spring Singleton范围的确切含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在applicationContext.xml中为同一个类定义了两个具有不同ID名称的bean.它工作得很好,所以让我感到困惑的是确切的spring singleton范围是什么意思.我的代码如下.我在stackoverflow中经历了其他一些类似的问题.但是我不清楚,因为那些上下文不完全相同,解释也很长.

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

我读到了
1.容器(上下文)的单个实例,
-就我而言,是否为我的两个实例创建了两个容器?<​​br>

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>

推荐答案

Singleton范围化的Bean很简单:每个Bean实际上都是在容器(即应用程序上下文)中实例化的一个对象.

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

在您的示例中,Spring容器将创建2个Employee对象(也称为Bean),分别是ee1.

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

其他作用域则比较棘手.一个bean可以实例化多个对象.例如,如果您定义了具有原型范围的bean,则每当您从容器中查找该bean时,容器都会实例化一个新的对象实例.对于其他作用域,情况类似:容器将根据特定时间创建一个新实例(例如,请求作用域将导致为每个传入请求创建一个bean等).

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

请注意:请勿将此Singleton与Singleton Design模式混淆.除了仅一个"特征外,它们没有其他共同点. Singleton Design Pattern具有许多其他特征(例如,全局可用的查找方法;整个过程空间中只有一个实例;不允许显式实例化等),这些特征与Spring的Singleton范围无关.

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

这篇关于Spring Singleton范围的确切含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆