Spring 会话作用域 bean 是否保存在 HttpSession 中?
因为我对 Spring 会话范围实现没有深入的了解。 谁能告诉我使用 Spring Session 作用域 bean 是否明智,其中 HttpSession 对象非常重要。就像一个 Web 应用程序,成千上万的用户同时访问该网站。
spring session作用域bean是否保存在HttpSession对象中?
或者即使 HttpSession 对象仅引用 spring session 范围的 bean,我们是否不会使 session 对象变得沉重?
将任何 bean 直接存储在 HttpSession 对象中(使 HttpSession 对象成为重磅观点)有何不同?
Since I don't have in depth knowledge of spring session scope implementation.
Can anyone please tell me if it is wise to use Spring Session scoped beans, where HttpSession object is very crucial. Like a web application where thousands of users access the site simultaneously.
Is spring session scoped bean saved in HttpSession object?
Or even if HttpSession object only refers to the spring session scoped bean, are we not making session object heavy?
How is it different form storing any bean directly in HttpSession object (making HttpSession object heavy point of view)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该对象并不真正存储在 HTTP 会话中。它与会话 ID 链接,实际上存储在 Spring 上下文中。会话侦听器用于在会话关闭后清理实例。请参阅 SessionScope< /a> JavaDoc.
The object is not really stored in HTTP session. It is linked with session id and actually stored inside of the Spring context. A session listener is used to clean instances once session is closed. See SessionScope JavaDoc.
Spring 文档是这样说的:
“重的”?不比您放入其中的物体重。会话应该有有限的生命周期。每个会话几KB并不意味着您的应用程序就结束了。对同时会话数和对象内存要求的简单计算应该可以让您对应用程序服务器内存设置放心。如果需要,您可以随时增加最小和最大内存。
无论您是否是 Spring bean,在 HTTP 会话中存储内容都是一样的。 bean 工厂只是做一些额外的事情来帮助您管理对象生命周期。
Here's what the Spring docs say:
"Heavy"? No heavier than the object you're putting in it. Sessions should have a finite lifetime. A few kbytes per session won't be the end of your application. A simple calculation for the number of simultaneous sessions and object memory requirements should reassure you about your app server memory settings. You can always increase min and max memory if needed.
Storing things in HTTP session happens the same whether you're a Spring bean or not. The bean factory just does some extra things to help manage the object lifecycle for you.
spring session作用域bean是否保存在HttpSession对象中?
如果你在这里提到的HttpSession对象实际上是由spring-session提供的(spring-session将httpSession对象包装在原始httpRequest中),那么答案是YES - >事实上,所有 spring session 作用域的 bean 都作为属性保存在 spring-session 提供的 httpSession 中。
或者即使 HttpSession 对象仅引用 spring session 作用域 bean,我们是否不会使 session 对象变得很重?
不。httpSession 的“重量”很大程度上取决于您放入的对象。另外、会话 bean 是短暂的。最后但并非最不重要的一点是,微服务世界中的会话对象通常被卸载到独立存储中,例如 Redis 或 HazelCast。因此,就内存消耗而言,它不会被视为“重”。
直接在 HttpSession 对象中存储任何 bean 有何不同(使 HttpSession 对象变得很重)?
完全没有区别(假设您使用的是 spring-session),因为所有 spring 会话作用域 bean 作为属性保存在 spring-session 提供的 httpSession 中。
Is spring session scoped bean saved in HttpSession object?
If the HttpSession object you mentioned here is actually provided by spring-session (spring-session wraps the httpSession object in original httpRequest), the answer would be YES -> indeed, all the spring session scoped beans are saved in the httpSession provided by spring-session as attributes.
Or even if HttpSession object only refers to the spring session scoped bean, are we not making session object heavy?
No. The "heaviness" of the httpSession very much depends on the objects you put in. In addtion, session beans are meant to be ephemeral. Last but not least, the session object in micro-services world is usually offload to a stand-alone store, like Redis or HazelCast. So, it won't be considered as "heavy" in terms of memory consumption.
How is it different form storing any bean directly in HttpSession object (making HttpSession object heavy point of view)?
No difference at all (assuming you are using spring-sesion), as all the all the spring session scoped beans are saved in the httpSession provided by spring-session as attributes.