小编典典

Spring bean范围

spring-mvc

有人可以解释一下我一直只使用“原型”的Spring bean的作用域吗,但是还有其他参数可以代替吗?

我在说什么的例子

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

阅读 675

收藏
2020-06-01

共1个答案

小编典典

Spring规范开始,支持五种类型的bean作用域:

1.单身人士(默认*)

每个Spring IoC容器将单个bean定义的作用域限定为单个对象实例。

2.原型

将单个bean定义的作用域限定为任意数量的对象实例。

3.要求

将单个bean定义的范围限定为单个HTTP请求的生命周期;也就是说,每个HTTP请求都将在单个bean定义的后面创建自己的bean实例。仅在可感知网络的Spring
ApplicationContext上下文中有效。

4.会议

将单个bean定义的范围限定为HTTP会话的生命周期。仅在可感知网络的Spring ApplicationContext上下文中有效。

5.全球会议

将单个bean定义的作用域限定为全局HTTP会话的生命周期。通常仅在portlet上下文中使用时才有效。仅在可感知网络的Spring
ApplicationContext上下文中有效。

  • default表示在<bean />标记中未明确提供范围的情况。在这里阅读有关它们的更多信息:http : //static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s04.html
2020-06-01