目录
一、什么是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} prototype | Scopes a single bean definition to any number of object instances.(和 singleton 想反,每次从Spring容器获取对象,都是获得一个新的实例。) |
r e q u e s t \color{#FF0000}{request} request | Scopes 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} session | Scopes 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} application | Scopes 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} websocket | Scopes 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"/>