Spring Bean Scopes作用域

当定义一个Spring的<bean>,必须声明bean 作用域的选项。例如,要强制Spring需要产生一个新的bean实例,应该声明bean的scope属性为prototype。如果你希望Spring 每次都返回同一个bean实例,应该声明bean的作用域,方式类似属性是单例。

Spring框架支持以下五个作用域,其中三个只有当您使用Web感知的 ApplicationContext 可用。

范围描述
singletonThis scopes the bean definition to a single instance per Spring IoC container (default).
prototypeThis scopes a single bean definition to have any number of object instances.
requestThis scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
sessionThis scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
global-sessionThis scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

本章将讨论前两个范围和其余三将讨论的时候,我们将讨论有关Web感知Spring的ApplicationContext。

singleton作用域:

如果范围设置为单例,Spring IoC容器创建了一个由该bean定义的对象只有一个实例。这个单一实例存储在这样的单例bean的高速缓存,以及所有后续请求和引用针对该bean返回缓存对象。

转载于:https://my.oschina.net/u/3611008/blog/2246943

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值