Java bean 的 Spring 会话范围究竟是如何工作的?Web 上下文中 bean 的默认范围是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29413290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:57:39  来源:igfitidea点击:

How exactly works the Spring session scope of a bean? what is the default scope of a bean in the web context?

javaspringspring-mvc

提问by AndreaNobili

I am studying Spring MVC and I have the following doubts:

我正在学习Spring MVC,我有以下疑问:

  1. What exactly is the purpose of the session scope?
  1. 会话范围的目的究竟是什么?

Reading the documentation I know that this scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext. And also that a new instance is created once per user session.

阅读文档我知道这将 bean 定义范围限定为 HTTP 会话。仅在 web-aware Spring ApplicationContext 的上下文中有效。而且每个用户会话都会创建一个新实例。

But when exactly is it used? and for what purpose? Can you make a practical example?

但是具体什么时候使用呢?目的是什么?你能举一个实际的例子吗?

  1. In Spring MVC what is the default scope in the web context?
  1. 在 Spring MVC 中,Web 上下文中的默认范围是什么?

I know that in Spring the default scope for a bean is singletonbut what about the scope of a bean in the web context?

我知道在 Spring 中,bean 的默认范围是单例,但是 web 上下文中 bean 的范围呢?

采纳答案by sachin k

Ans 1) session scope is very similar to HttpSession scope. Beans instantiated based on session scope scope lives through the HTTP session. Similar to request scope, it is applicable only for web aware spring application contexts.

Ans 1) 会话范围与 HttpSession 范围非常相似。基于会话作用域实例化的 Bean 存在于 HTTP 会话中。与请求范围类似,它仅适用于 Web 感知 Spring 应用程序上下文。

/** * Annotation-based configuration of session scope */ 
@Component
@Scope("session") 
public class ShopCart { }

and then

进而

@Inject
private ShopCart cart;

Ans 2) Default is Singleton everywhere.

Ans 2) 处处默认为单例。

回答by Vojtech Ruzicka

  1. You use spring session beans for beans which are stateful and their state differs per user. These can be for example preferences of currently logged in user.
  2. Default scope of bean in spring is singleton and it is no different in Web Application context.
  1. 对于有状态且状态因用户而异的 bean,您可以使用 spring 会话 bean。例如,这些可以是当前登录用户的偏好。
  2. spring 中 bean 的默认范围是单例,它在 Web 应用程序上下文中没有什么不同。

Note than in web environment you can use also REQUEST scoped beans and their lifetime is only per one user request. You should use request scope when session is not necessary and request is sufficient.

请注意,在 Web 环境中,您还可以使用 REQUEST 范围的 bean,它们的生命周期仅针对一个用户请求。当不需要会话并且请求足够时,您应该使用请求范围。

Also, in portlet environment, you can use another scope which is GLOBAL SESSION. Each portlet has its own indepenednt session and typically those portlets are preffered to have their own state encapsulated only for themselves. But if you need to share session data among different portlets, you will need to use global session scope.

此外,在 portlet 环境中,您可以使用另一个范围,即 GLOBAL SESSION。每个 portlet 都有自己的独立会话,并且通常这些 portlet 倾向于将它们自己的状态封装起来,只为它们自己封装。但是如果您需要在不同的 portlet 之间共享会话数据,您将需要使用全局会话范围。

回答by Yasir Shabbir Choudhary

Actually Spring help you create Session scope bean instead traditional way

实际上 Spring 帮助您创建会话范围 bean 而不是传统方式

httpSession.setAttribute("Object",new Object());
&&
httpSession.getAttribute("Object");

and Spring provide this efficient way

和 Spring 提供了这种有效的方式

@Component
@Scope("session")
public class Foo{
}

now it's headache of spring to create and destroy this associated session object using Factory Pattern

现在使用工厂模式创建和销毁这个关联的会话对象是 spring 的头痛问题

回答by Roberto Rodriguez

I had the same problem, I was using:

我有同样的问题,我正在使用:

@Component
@Scope("session")

And this made the magic for me:

这对我来说很神奇:

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) 

I hope it helps ;-)

我希望它有帮助;-)

回答by Sabir Khan

But when exactly is it used? and for what purpose? Can you make a practical example?

但是具体什么时候使用呢?目的是什么?你能举一个实际的例子吗?

In one of my JSP based Spring MVC web apps, we use it to store data that doesn't vary after user's first request in the session i.e. we populate fields of this bean when user first comes to server & then we use ( aka read ) these values in subsequent requests ( next requests in session ) e.g. user name, user org group, address, logged in client numberetc etc.

在我的一个基于 JSP 的 Spring MVC Web 应用程序中,我们使用它来存储在用户在会话中的第一个请求之后不会改变的数据,即我们在用户第一次访问服务器时填充此 bean 的字段,然后我们使用(又名读取)后续请求(会话中的下一个请求)中的这些值,例如用户名用户组织组地址登录的客户端编号等。

These constants values are kind of mandatory & be needed in all log messages or in all SQL queries.

这些常量值是强制性的,在所有日志消息或所有 SQL 查询中都需要。

Request routing is designed in such a way that bean population ( setting of bean properties ) happens only once per session.

请求路由的设计方式是 bean 填充(bean 属性的设置)每个会话仅发生一次。

Other interesting part as pointed in Yasir Shabbir Choudhary's answer that you can mimic same behavior using traditional way.

Yasir Shabbir Choudhary 的回答中指出的其他有趣部分,您可以使用传统方式模仿相同的行为。

Is Spring session scoped bean saved in HttpSession?

Spring 会话范围的 bean 是否保存在 HttpSession 中?

Your second question is already answered by many that default scope - Singletonis applicable here too.

许多默认范围已经回答了您的第二个问题 -单例也适用于此。