Bean作用域.
Scope | Description |
---|---|
singleton | (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. (默认情况下)将每个Spring IoC容器的单个bean定义定位到单个对象实例。 |
prototype | Scopes a single bean definition to any number of object instances.将单个bean定义作用于任意数量的对象实例。 |
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 .将单个bean定义定位到单个HTTP请求的生命周期。也就是说,每个HTTP请求都有它自己的bean实例,该实例是在单个bean定义的背面创建的。仅在感知web的Spring“ApplicationContext”上下文中有效。 |
session | Scopes a single bean definition to the lifecycle of an HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext .将单个bean定义作用于HTTP“会话”的生命周期。仅在感知web的Spring“ApplicationContext”上下文中有效。 |
application | Scopes a single bean definition to the lifecycle of a ServletContext . Only valid in the context of a web-aware Spring ApplicationContext .将单个bean定义作用于“ServletContext”的生命周期。仅在感知web的Spring“ApplicationContext”上下文中有效。 |
websocket | Scopes a single bean definition to the lifecycle of a WebSocket . Only valid in the context of a web-aware Spring ApplicationContext .将单个bean定义作用于“WebSocket”的生命周期。仅在感知web的Spring“ApplicationContext”上下文中有效。 |
1、单例模式(singleton
:默认)
<bean id="" class="" scope="singleton"/>
2、原型模式(prototype
):每次从容器中getBean,获取的都是一个新对象
<bean id="" class="" scope="prototype"/>
3、其余的request,session,application只能在web开发中使用到