Spring Bean Scopes做用域

当定义一个Spring的<bean>,必须声明bean 做用域的选项。例如,要强制Spring须要产生一个新的bean实例,应该声明bean的scope属性为prototype。若是你但愿Spring 每次都返回同一个bean实例,应该声明bean的做用域,方式相似属性是单例。web

Spring框架支持如下五个做用域,其中三个只有当您使用Web感知的 ApplicationContext 可用。缓存

范围 描述
singleton This scopes the bean definition to a single instance per Spring IoC container (default).
prototype This scopes a single bean definition to have any number of object instances.
request This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
session This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
global-session This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

本章将讨论前两个范围和其他三将讨论的时候,咱们将讨论有关Web感知Spring的ApplicationContext。session

singleton做用域:框架

若是范围设置为单例,Spring IoC容器建立了一个由该bean定义的对象只有一个实例。这个单一实例存储在这样的单例bean的高速缓存,以及全部后续请求和引用针对该bean返回缓存对象。prototype

相关文章
相关标签/搜索