Bean的作用域
Scope | Description |
---|---|
singleton | (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. |
prototype | Scopes a single bean definition to any number of object instances. |
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 . |
session | Scopes a single bean definition to the lifecycle of an HTTP Session . Only valid in the context of a web-aware 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 . |
websocket | Scopes a single bean definition to the lifecycle of a WebSocket . Only valid in the context of a web-aware Spring ApplicationContext . |
singleton
在spring中默认的是singleton
<!-- 默认也是单例模式-->
<bean id="user" class="com.wjq.pojo.User" scope="singleton">
<property name="name" value="aaa"/>
<property name="age" value="20"/>
</bean>
prototype
<bean id="user" class="com.wjq.pojo.User" scope="prototype">
<property name="name" value="aaa"/>
<property name="age" value="20"/>
</bean>
其他作用域是在web中使用。