bean配置之scope
Scope | 描述 |
---|---|
singleton | (默认)将单个 bean 定义限定为每个 Spring IoC 容器的单个对象实例。 |
prototype | 将单个 bean 定义限定为任意数量的对象实例。 |
request | 将单个 bean 定义限定为单个 HTTP 请求的生命周期。也就是说,每个 HTTP 请求都有自己的 bean 实例,该实例是在单个 bean 定义的后面创建的。仅在 Web 感知 Spring 的上下文中有效ApplicationContext。 |
session | 将单个 bean 定义限定为 HTTP 的生命周期Session。仅在 Web 感知 Spring 的上下文中有效ApplicationContext。 |
application | 将单个 bean 定义限定为ServletContext. 仅在 Web 感知 Spring 的上下文中有效ApplicationContext。 |
websocket | 将单个 bean 定义限定为WebSocket. 仅在 Web 感知 Spring 的上下文中有效ApplicationContext。 |
配置示例
<bean id="userDao" class="com.example.demo.dao.Impl.UserDaoImpl" scope="singleton"></bean>
或者
<bean id="userDao" class="com.example.demo.dao.Impl.UserDaoImpl" scope="prototype"></bean>