




在Spring框架中,Bean的作用域(Scope)决定了Bean的生命周期和可见性。常见的作用域包括singleton、prototype、request、session和application。你提到的request、session和global session作用域主要用于Web应用程序。下面详细介绍这些作用域及其用途,帮助你更好地理解和应用它们。




1. Spring Bean的作用域概述



Singleton(单例)
Prototype(原型)
Request
Session
Global Session




2. 详细介绍各个作用域




a. Request作用域
定义:
Bean的生命周期绑定到一次HTTP请求。
在一次请求中,无论调用多少次,都返回同一个Bean实例。
请求结束后,Bean实例被销毁。

配置方法:
public class RequestScopedBean {// Bean内容}
<bean id="requestScopedBean" class="com.example.RequestScopedBean" scope="request"/>使用场景:
处理与单个HTTP请求相关的逻辑。
如表单处理、请求参数绑定等。

注意事项:
仅在Web应用环境下有效(如Spring MVC)。
使用时需要确保Bean的依赖也支持相应的作用域,通常需要使用代理模式(proxyMode = ScopedProxyMode.TARGET_CLASS)来避免作用域冲突。


b. Session作用域
定义:
Bean的生命周期绑定到一个HTTP会话。
在一个会话中,无论多少请求,都返回同一个Bean实例。
会话结束后,Bean实例被销毁。

配置方法:
public class SessionScopedBean {// Bean内容}
<bean id="sessionScopedBean" class="com.example.SessionScopedBean" scope="session"/>使用场景:
保持用户会话期间的状态信息。
如用户认证信息、购物车、用户偏好设置等。

注意事项:
仅在Web应用环境下有效。
需要使用代理模式,确保Bean在不同请求中能正确引用会话作用域的实例。


c. Global Session作用域
定义:
Bean的生命周期绑定到一个全局会话,通常用于Portlet应用。
一个全局会话中,无论多少Portlet请求,都返回同一个Bean实例。
全局会话结束后,Bean实例被销毁。

配置方法:
public class GlobalSessionScopedBean {// Bean内容}
<bean id="globalSessionScopedBean" class="com.example.GlobalSessionScopedBean" scope="globalSession"/>使用场景:
主要用于Portlet环境,用于多个Portlet共享同一个全局会话状态。

注意事项:
仅在Portlet应用环境下有效,不适用于标准的Servlet-based Web应用。
Spring Boot项目中通常不涉及Portlet,因此不常见。




3. 为什么你可能没见过这些作用域



非Web应用:
默认配置:
依赖缺失:
开发实践:




4. 如何在Spring Boot项目中使用Web作用域



如果你正在使用Spring Boot开发Web应用,并希望使用request或session作用域,可以按照以下步骤配置:

a. 添加Web依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

b. 配置Bean的作用域
import org.springframework.context.annotation.Scope;import org.springframework.context.annotation.ScopedProxyMode;import org.springframework.stereotype.Component;import org.springframework.web.context.WebApplicationContext;public class RequestScopedBean {// Bean内容}public class SessionScopedBean {// Bean内容}

c. 使用Bean
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;public class SomeService {private RequestScopedBean requestScopedBean;private SessionScopedBean sessionScopedBean;// 使用这些Bean的方法}



5. 示例应用



// RequestScopedBean.javaimport org.springframework.context.annotation.Scope;import org.springframework.context.annotation.ScopedProxyMode;import org.springframework.stereotype.Component;import org.springframework.web.context.WebApplicationContext;public class RequestScopedBean {private String data = "Request Scoped Data";public String getData() {return data;}}// SessionScopedBean.javaimport org.springframework.context.annotation.Scope;import org.springframework.context.annotation.ScopedProxyMode;import org.springframework.stereotype.Component;import org.springframework.web.context.WebApplicationContext;public class SessionScopedBean {private String data = "Session Scoped Data";public String getData() {return data;}}// SomeController.javaimport org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;public class SomeController {private RequestScopedBean requestScopedBean;private SessionScopedBean sessionScopedBean;public String testScopes() {return "RequestScopedBean: " + requestScopedBean.getData() +", SessionScopedBean: " + sessionScopedBean.getData();}}
每次访问/test端点时,RequestScopedBean都会创建一个新的实例。
对于同一个会话中的多次访问,SessionScopedBean会保持同一个实例。



👋 大家好,我是枫哥,一名Java后端开发者!我热衷于探索新技术,并在我的微信公众号上分享关于Java 生态和后端开发的知识。欢迎关注我的公众号,期待与你一起探讨技术的无穷可能!” 目前专注于Java技术分享,覆盖春招、秋招、社招和跳槽相关内容,并提供一对一带徒学习服务。加入 学徒计划,即可享受内推机会和优质资源,签订协议确保就业无忧。此外,我们还推出了‘Java跳槽网’, 为你的求职之路提供全方位支持,助你快速找到理想工作!



原文作者:IT枫斗者;如有侵权,请联系管理员删除。