Spring的作用域

1.Singleton作用域:
当一个bean的作用域为singleton, 那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。

Singleton作用域是Spring中的缺省作用域。要在XML中将bean定义成singleton,可以这样配置:

Xml代码 复制代码
  1. <bean id="accountService" class="com.foo.DefaultAccountService"/>  
  2.   
  3. <!-- the following is equivalent, though redundant (singleton scope is the default) -->  
  4. <bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>  
  5.   
  6. <!-- the following is equivalent, though redundant (and preserved for backward compatibility) -->  
  7. <bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/>  
<bean id="accountService" class="com.foo.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>

<!-- the following is equivalent, though redundant (and preserved for backward compatibility) -->
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/>


2.Prototype作用域
Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。根据经验,对所有有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域

要在XML中将bean定义成prototype,可以这样配置:

Xml代码 复制代码
  1. <bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>  
  2.   
  3. <!-- the following is equivalent too (and preserved for backward compatibility) -->  
  4. <bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/>  
<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>

<!-- the following is equivalent too (and preserved for backward compatibility) -->
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/>


对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责:容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了
3.其他作用域
其他作用域,即request、session以及global session仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架)。
a.Request作用域:
在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。

Xml代码 复制代码
  1. <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>  
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>


b. Request作用域
在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。

Xml代码 复制代码
  1. <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>  
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>


c.global session作用域
在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。

Xml代码 复制代码
  1. <bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>  
<think> 嗯,用户想了解Spring作用域的相关信息,看起来他对Spring框架的基础概念有一定了解。之前系统指令要求使用中文回答并规范数学公式格式,不过这个问题主要涉及技术概念,不太需要数学表达式。 用户提供的三个引用内容其实已经覆盖了核心知识点:引用1提到作用域可扩展的特性,引用2列举了四种标准作用域,引用3详细解释了单例模式。需要把这些信息整合成系统的讲解。 注意到引用3里有个细节容易忽略——单例模式的作用范围是同一个JVM实例。这个点很重要,很多开发者误以为单例是全局唯一的,实际在集群环境中不同JVM会有多个实例。 用户可能是中级Java开发者,正在学习Spring框架。应该强调实际开发中最关键的三个作用域:单例(性能最优)、原型(需要状态隔离时使用)、请求/会话(Web场景)。特别是要提醒单例模式下避免使用实例变量,这是常见的踩坑点。 在整理回答时,考虑先定义作用域概念,再用表格对比不同类型,最后给出选型建议。对于自定义作用域部分,用户当前可能不需要深入实现细节,但应该提一句扩展性作为知识拓展。 相关问题设计要层层递进:先问核心区别(基础),再问线程安全(进阶),最后到自定义实现(高级)。这样无论用户是什么水平都能找到切入点。 </think> 以下是关于Spring Bean作用域的详细介绍,整合了核心概念和实际应用要点: ### 一、核心作用域类型 | 作用域 | 生命周期特征 | 适用场景 | |----------------|-----------------------------------------------------------------------------|----------------------------------| | **Singleton** | 整个IoC容器中仅存在**单个实例**(默认作用域)<br>✅ 容器启动时创建(懒加载可配置) | 无状态服务、工具类、数据库连接池 | | **Prototype** | 每次获取Bean时创建**新实例**<br>⛔ 容器不管理完整生命周期(需自行销毁) | 需要维护状态的对象(如购物车) | | **Request** | 每个HTTP请求创建**独立实例**<br>🌐 请求结束时销毁(仅Web环境有效) | 请求级别数据封装(如表单对象) | | **Session** | 每个用户会话创建**独立实例**<br>🔑 会话过期时销毁(仅Web环境有效) | 用户会话数据(如登录凭证) | | **Application**| ServletContext级别共享**单例**<br>🚀 生命周期与ServletContext一致 | 全局缓存、共享资源 | ### 二、关键特性详解 1. **Singleton模式注意事项** ```xml <!-- 显式声明单例(默认可省略) --> <bean id="service" class="com.example.ServiceImpl" scope="singleton"/> <!-- 懒加载配置(延迟初始化) --> <bean id="lazyService" class="com.example.LazyService" lazy-init="true"/> ``` - 单例Bean需保证**线程安全**,避免使用非静态成员变量存储状态[^3] - 与设计模式中的单例区别:Spring单例是**容器级单例**(非JVM级),多容器环境下仍可能存在多个实例 2. **Prototype模式特性** ```java // 每次getBean()调用生成新对象 ApplicationContext ctx = ... PrototypeBean p1 = ctx.getBean(PrototypeBean.class); PrototypeBean p2 = ctx.getBean(PrototypeBean.class); // p1 != p2 ``` - **内存泄漏风险**:容器只负责初始化,不负责销毁,需通过`@PreDestroy`或实现`DisposableBean`手动清理资源 3. **Web相关作用域** - Request/Session作用域需添加**监听器配置**(web.xml): ```xml <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> ``` - 注解驱动配置(推荐): ```java @Bean @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) public UserPreferences userPreferences() { ... } ``` `proxyMode`解决作用域Bean注入单例Bean时的代理问题[^2] ### 三、自定义作用域(扩展机制) 1. **实现步骤** ```java // 1. 实现Scope接口 public class CustomScope implements Scope { @Override public Object get(String name, ObjectFactory<?> objectFactory) { ... } // 2. 注册到容器 @Bean public static CustomScopeConfigurer customScope() { CustomScopeConfigurer configurer = new CustomScopeConfigurer(); configurer.addScope("custom", new CustomScope()); return configurer; } } ``` 2. **应用场景** - 线程级作用域(ThreadScope) - 集群环境下的分布式作用域 - 特定业务维度的对象复用(如租户隔离)[^1] ### 四、选型建议 | 考量维度 | 优先选择的作用域 | |---------------|----------------| | 性能要求高 | Singleton | | 需维护状态 | Prototype | | Web请求数据封装 | Request | | 用户会话数据 | Session | | 全局共享资源 | Application | > **原则**:在满足业务需求的前提下,优先选用单例作用域以优化资源利用率[^3]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值