Spring Bean Scopes [英] Spring Bean Scopes
问题描述
有人可以解释一下Spring bean中的范围是什么我总是只使用'原型'但是我可以用其他参数代替吗?
Can someone explain what the scopes are in Spring beans I've always just used 'prototype' but are there other parameters I can put in place of that?
我正在谈论的例子
<bean id="customerInfoController" class="com.action.customer.Controller" scope="prototype">
<property name="accountDao" ref="accountDao"/>
<property name="utilityDao" ref="utilityDao"/>
<property name="account_usageDao" ref="account_usageDao"/>
</bean>
推荐答案
来自spring specs ,支持五种类型的bean范围:
From the spring specs, there are five types of bean scopes supported :
1。 singleton(默认*)
根据Spring
IoC容器将单个bean定义范围扩展到单个对象实例。
Scopes a single bean definition to a single object instance per Spring IoC container.
2。原型
将单个bean定义范围限定为任意数量的对象实例。
Scopes a single bean definition to any number of object instances.
3。请求
将单个bean定义范围限定为单个HTTP
请求的生命周期;也就是说,每个HTTP请求都有自己的
实例,该实例是在单个bean定义的后面创建的。
仅在支持Web的Spring ApplicationContext上下文中有效。
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
4。会话
将单个bean定义范围限定为HTTP会话的生命周期。
仅在支持Web的Spring ApplicationContext上下文中有效。
Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
5。全局会话
将单个bean定义范围限定为全局HTTP
会话的生命周期。通常仅在portlet上下文中使用时有效。只有
在Web感知Spring ApplicationContext的上下文中有效。
Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
* default表示未明确提供范围时< bean />
标记。
在这里阅读更多关于它们的信息: http:// static。 springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s04.html
*default means when no scope is explicitly provided in the <bean />
tag.
read more about them here: http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s04.html
这篇关于Spring Bean Scopes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!