Spring Bean 范围 [英] 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.Controller" scope="prototype">
<property name="accountDao" ref="accountDao"/>
<property name="utilityDao" ref="utilityDao"/>
<property name="account_usageDao" ref="account_usageDao"/>
</bean>
推荐答案
来自 spring 规范,支持五种类型的 bean 作用域:
From the spring specs, there are five types of bean scopes supported :
1.单例(默认*)
将单个 bean 定义范围限定为每个 Spring 的单个对象实例IoC 容器.
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 定义的后面创建的 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 范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!