官方文档上提到Spring中scope属性可以有以下取值:本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
1. singleton : (Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
singleton (单一实例)容器中创建时只存在一个实例,也就是单例模型。本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
2. prototype : Scopes a single bean definition to any number of object instances.
prototype 容器在输出bean对象时,每次都会重新生成一个新的对象给请求方。本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
request,session和global session只用于web程序,比如和XmlWebApplicationContext共同使用。
3. 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.
request 容器XmlWebApplicationContext 会为每个HTTP请求创建一个全新的RequestPrecessor对象,请求结束该对象的生命周期结束,可以认为在浏览器每次刷新都会生成一个新的bean。本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
4. session : Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
session 可以理解为用户的登录信息,Spring容器会为每个独立的session创建属于自己的全新的实例,比request存活更长时间。可以理解为一个用户使用浏览器请求每次刷新时都是相同的bean.本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
5. application : Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
ServletContext(web应用)中的单例,在一个web应用中,可能会有多个ApplicationContext,所以,application不等于singleton(singleton是容器中的单例)。可以理解为不同的用户刷新同一个请求(调用同一个application)都是相同的bean.本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
6. websocket : Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext。即WebSocket的完整生命周期中将创建并提供一个实例。本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
虽然属性值不少,不过常用的也就是singleton和prototype。除了在配置文件中设置scope属性外,还可以在类中使用scope注解实现。如下示例,而如果在非WEB程序中使用request就会报异常:Exception in thread "main" java.lang.IllegalStateException: No Scope registered for scope name 'request'本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
package com.kermit.test; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("prototype") //@Scope("singleton") //@Scope("request") public class HelpTool { public String getString(){ return "spring."; } }本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
另外注意到spring文档中1.5.3.关于Singleton Beans with Prototype-bean Dependencies的描述:本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
When you use singleton-scoped beans with dependencies on prototype beans, be aware that dependencies are resolved at instantiation time. Thus, if you dependency-inject a prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated and then dependency-injected into the singleton bean. The prototype instance is the sole instance that is ever supplied to the singleton-scoped bean.本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
However, suppose you want the singleton-scoped bean to acquire a new instance of the prototype-scoped bean repeatedly at runtime. You cannot dependency-inject a prototype-scoped bean into your singleton bean, because that injection occurs only once, when the Spring container instantiates the singleton bean and resolves and injects its dependencies. If you need a new instance of a prototype bean at runtime more than once, see Method Injection。本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
即要在一个singleton的bean中注入一个prototype的bean,这个prototype的bean只会初始化一次,因为在注入操作时prototype的bean已经完成了初始化,因为prototype的作用不会生效。如果要让这prototype生效,可以使用Method Injection方法注入。本文地址:http://www.04007.cn/article/909.html,未经许可,不得转载.
本文地址:http://www.04007.cn/article/909.html 未经许可,不得转载. 手机访问本页请扫描右下方二维码.
![]() |
![]() |
手机扫码直接打开本页面 |