春天默认范围是单身吗? [英] Is spring default scope singleton or not?

查看:141
本文介绍了春天默认范围是单身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能否解释为什么Spring正在为以下配置的bean创建两个对象,因为默认情况下,spring的默认范围是单例?



Spring配置在这里:

 < bean id =customerclass =jp.ne.goo.beans.Customer> 
< property name =custnovalue =100>< / property>
< property name =custNamevalue =rajasekhar> < /性>
< / bean>
< bean id =customer2class =jp.ne.goo.beans.Customer>
< property name =custnovalue =200>< / property>
< property name =custNamevalue =siva>< / property>
< / bean>


解决方案

Spring的默认范围单身。只是你的想法是什么意思是单身人士不符合Spring如何定义单身人士。



如果您告诉Spring制作两个不同名称的单独的bean,同一个类,那么你得到两个单独的bean,每个都有单例范围。所有单例范围意味着当您引用相同ID的内容时,您将获得相同的bean实例。



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


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


单例范围意思是使用相同的id检索同一个bean,就是所有。测试没有两个id引用同一个类将会妨碍使用map作为bean,并且通过使用BeanFactories代理东西将变得复杂。
对于Spring来说,这将涉及到大量的工作,但没有什么好处,而是相信用户知道他们在做什么。



为同一个bean定义两个名称是使用别名


在bean定义本身中,您可以为该bean提供多个名称,通过使用id属性指定的最多一个名称的组合以及name属性中的任意数量的其他名称。这些名称可以是同一个bean的等价别名,并且对于某些情况是有用的,例如允许应用程序中的每个组件通过使用特定于该组件本身的bean名称引用常见的依赖关系。



然而,指定bean实际定义的所有别名并不总是足够的。有时候,为其他地方定义的bean引入别名。这在大型系统中通常是这样,其中配置在每个子系统之间分配,每个子系统具有其自己的一组对象定义。在基于XML的配置元数据中,您可以使用该元素来实现此目的。


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

 < bean id =customername =customer2
class =jp.ne.goo。 beans.Customer>
< / bean>

或为其他位置定义的bean创建一个别名:

 < alias name =customeralias =customer2/> 

那么customer和customer2将引用同一个bean实例。


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?

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

If you tell Spring to make two separate beans with different names 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.

Here is how the Spring documentation defines singleton scope:

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

The way to define two names for the same bean is to use an alias:

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.

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.

So if you add a name in the bean configuration:

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

or create an alias for a bean defined elsewhere:

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

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

这篇关于春天默认范围是单身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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