spring 默认作用域是否是单例? [英] Is spring default scope singleton or not?

查看:36
本文介绍了spring 默认作用域是否是单例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否解释一下为什么 Spring 为如下所示的 bean 配置创建两个对象,因为默认情况下,spring 默认范围是单例?

Could you please explain why Spring is creating two objects for the configuration of beans shown below, since by default spring default scope is singleton?

Spring 配置在这里:

The Spring configuration is here:

<bean id="customer" class="jp.ne.goo.beans.Customer"> 
    <property name="custno" value="100"></property>
    <property name="custName" value="rajasekhar"> </property>
</bean>
<bean id="customer2" class="jp.ne.goo.beans.Customer"> 
    <property name="custno" value="200"></property> 
    <property name="custName" value="siva"></property> 
</bean>

推荐答案

Spring 的默认范围 单例.只是你对单身意味着什么的想法与 Spring 对单身的定义不符.

Spring's default scope is singleton. It's just that your idea of what it means to be a singleton doesn't match how Spring defines singletons.

如果您告诉 Spring 使用不同的 id 和相同的类创建两个单独的 bean,那么您将得到两个单独的 bean,每个 bean 都有单例作用域.所有的单例作用域都意味着当你引用具有相同 id 的东西时,你会得到相同的 bean 实例.

If you tell Spring to make two separate beans with different ids and the same class, then you get two separate beans, each with singleton scope. All singleton scope means is that when you reference something with the same id, you get the same bean instance back.

这里是如何Spring 文档定义了单例范围:

仅管理一个单例 bean 的一个共享实例,并且所有对具有与该 bean 定义匹配的 id 或 ids 的 bean 的请求都会导致 Spring 容器返回一个特定的 bean 实例.

Only one shared instance of a singleton bean is managed, and all requests for beans with an id or ids matching that bean definition result in that one specific bean instance being returned by the Spring container.

Singleton 作用域意味着使用相同的 id 检索相同的 bean,仅此而已.测试没有两个 id 引用同一个类会妨碍将映射用作 bean,并且会因使用 BeanFactories 代理而变得复杂.对于 Spring 进行监管,这将涉及大量工作,但收效甚微.相反,它相信用户知道他们在做什么.

Singleton scope means using the same id retrieves the same bean, that is all. Testing that no two ids referenced the same class would get in the way of using maps as beans, and would be complicated by proxying things with BeanFactories. For Spring to police this would involve a lot of work for little benefit. Instead it trusts the users to know what they're doing.

如果您希望跨多个名称保留 bean 的单例性,这是可行的.您可以有多个名称引用同一个 bean,这是通过使用 别名:

If you want a bean’s singleton-ness preserved across multiple names, that is do-able. You can have more than one name refer to the same bean, that is done by using an alias:

在 bean 定义本身中,您可以通过使用 id 属性指定的最多一个名称和 name 属性中任意数量的其他名称的组合,为 bean 提供多个名称.这些名称可以是同一个 bean 的等效别名,并且在某些情况下很有用,例如允许应用程序中的每个组件通过使用特定于该组件本身的 bean 名称来引用公共依赖项.

In a bean definition itself, you can supply more than one name for the bean, by using a combination of up to one name specified by the id attribute, and any number of other names in the name attribute. These names can be equivalent aliases to the same bean, and are useful for some situations, such as allowing each component in an application to refer to a common dependency by using a bean name that is specific to that component itself.

然而,在实际定义 bean 的地方指定所有别名并不总是足够的.有时需要为在别处定义的 bean 引入别名.这在大型系统中很常见,其中配置在每个子系统之间拆分,每个子系统都有自己的一组对象定义.在基于 XML 的配置元数据中,您可以使用元素来完成此操作.

Specifying all aliases where the bean is actually defined is not always adequate, however. It is sometimes desirable to introduce an alias for a bean that is defined elsewhere. This is commonly the case in large systems where configuration is split amongst each subsystem, each subsystem having its own set of object definitions. In XML-based configuration metadata, you can use the element to accomplish this.

所以如果你在 bean 配置中添加一个名称:

So if you add a name in the bean configuration:

<bean id="customer" name="customer2" 
    class="jp.ne.goo.beans.Customer">
</bean>

或为别处定义的 bean 创建别名:

or create an alias for a bean defined elsewhere:

<alias name="customer" alias="customer2"/>

然后是客户"和客户 2"将引用同一个 bean 实例.

then "customer" and "customer2" will refer to the same bean instance.

这篇关于spring 默认作用域是否是单例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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