[Spring学习]05 Spring bean的作用域

一、什么是Spring bean

Spring 官方对 bean 的解释是:

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.

翻译过来就是:

在 Spring 中,构成应用程序主干并由Spring IoC容器管理的对象称为bean。bean是一个由Spring IoC容器实例化、组装和管理的对象。

二、Spring bean的作用域

Scope(范围)Description(描述)
s i n g l e t o n \color{#FF0000}{singleton} singleton(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.(一个 Spring 容器中仅有一个实例,即该 Bean 是单例存在的,是 Spring 中的默认方式)
p r o t o t y p e \color{#FF0000}{prototype} prototypeScopes a single bean definition to any number of object instances.(和 singleton 想反,每次从Spring容器获取对象,都是获得一个新的实例。)
r e q u e s t \color{#FF0000}{request} requestScopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has 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.(只在 web 项目中有效,同一个 http 请求中,获取的容器中的实例都是单例的。)
s e s s i o n \color{#FF0000}{session} sessionScopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.(只在 web 项目中有效,在每一次 session 的生命周期中,获取的容器中的实例都是单例的。)
a p p l i c a t i o n \color{#FF0000}{application} applicationScopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.(只在 web 项目中有效,在每一次 Servlet 上下文的生命周期中,获取的容器中的实例都是单例的。)
w e b s o c k e t \color{#FF0000}{websocket} websocketScopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.(只在 web 项目中有效,在每一次 WebSocket 的生命周期中,获取的容器中的实例都是单例的。)

1、singleton

单例模式,如果bean的作用域的属性被声明为singleton,Spring IoC容器中只会创建一个共享的Bean实例,无论有多少个Bean引用它,始终指向同一对象。注意:Singleton作用域是Spring中的缺省作用域。
在这里插入图片描述
可在 bean 的配置文件中设置作用域的属性为singleton,如下所示:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
</bean>

2、prototype

原型模式,如果bean的作用域的属性被声明为prototype,每次定义bean时,Spring IoC容器都会创建一个新的Bean实例,每个Bean实例都有自己的属性和状态,而singleton全局只有一个对象。
在这里插入图片描述
可在 bean 的配置文件中设置作用域的属性为prototype,如下所示:

<!-- A bean definition with prototype scope -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

3、request

Spring容器在接受到每个HTTP请求的时候都会创建一个新的请求实例。当http请求调用作用域为request的bean的时候,每增加一个HTTP请求,Spring就会创建一个新的bean,在请求处理完成之后便及时销毁这个bean。Spring容器根据该bean的定义创建一个全新的实例,且该实例仅在当前Http请求内有效,而其它请求无法看到当前请求中状态的变化。
该作用域仅在基于web的Spring ApplicationContext情形下有效。

可在 bean 的配置文件中设置作用域的属性为request,如下所示:

<!-- A bean definition with request scope -->
<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>

或:

@RequestScope
@Component
public class LoginAction {
    // ...
}

4、session

在一次Http Session中,容器会返回该Bean的同一实例。而对不同的Session请求则会创建新的实例,该bean实例仅在当前Session内有效。
同Http请求相同,每一次session请求创建新的实例,而不同的实例之间不共享属性,且实例仅在自己的session请求内有效,请求结束,则实例将被销毁。
该作用域仅在基于web的Spring ApplicationContext情形下有效。

可在 bean 的配置文件中设置作用域的属性为request,如下所示:

<!-- A bean definition with session scope -->
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

或:

@SessionScope
@Component
public class UserPreferences {
    // ...
}

5、application

application作用域仅仅作为ServletContext的属性可见。
该作用域仅适用于web的Spring WebApplicationContext环境。

可在 bean 的配置文件中设置作用域的属性为application,如下所示:

<!-- A bean definition with application scope -->
<bean id="appPreferences" class="com.foo.AppPreferences" scope="application"/>

6、websocket

在每一次 WebSocket 的生命周期中,获取的容器中的实例都是单例的。
该作用域仅适用于web的Spring WebApplicationContext环境。

可在 bean 的配置文件中设置作用域的属性为websocket,如下所示:

<!-- A bean definition with websocket scope -->
<bean id="appPreferences" class="com.foo.AppPreferences" scope="websocket"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

joinclear

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值