gpt4 book ai didi

java - Spring Bean 范围

转载 作者:IT老高 更新时间:2023-10-28 13:02:34 25 4
gpt4 key购买 nike

有人能解释一下 Spring bean 中的作用域是什么,我一直只使用“原型(prototype)”,但是我可以用其他参数代替它吗?

我所说的例子

<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 specs ,支持 5 种 bean 范围:

1. singleton(default*)

Scopes a single bean definition to a single object instance per Spring IoC container.

2. prototype

Scopes a single bean definition to any number of object instances.

3. request

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

Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

5. global session

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

关于java - Spring Bean 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17599216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com