Spring学习笔记-7-Bean作用域


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开发中使用到


文章作者: liuminkai
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 liuminkai !
评论
 上一篇
Spring学习笔记-8-Bean的自动装配 Spring学习笔记-8-Bean的自动装配
Bean的自动装配. 自动装配是Spring满足bean依赖一种方式 Spring会在上下文中自动寻找,并自动给bean装配属性 在Spring中有三种装配方式. 在xml中装配 在java中装配 隐式的自动装配(autowired):将
2020-07-27
下一篇 
Spring学习笔记-6-依赖注入 Spring学习笔记-6-依赖注入
依赖注入.目录结构 1、构造器注入.直接通过参数名(推荐) 还有两种(索引,和类型).<!--按名字 beans.xml--> <bean id="user" class="pojo.User"> <constr
2020-07-27
  目录