简体   繁体   English

spring 如何管理 session?

[英]How does spring manage session?

I have a small springboot app which gets a file uploaded, do some work on it.我有一个小型 springboot 应用程序,它上传了一个文件,做一些工作。 and returns back another file.并返回另一个文件。 I have implemented frontend using reactJS.我已经使用 reactJS 实现了前端。 There is a static variable which holds the name of file.有一个包含文件名的 static 变量。 It works fine with one file.它适用于一个文件。 But, when I throw multiple files at it, the file which gets uploaded at last overrides the static variable for each operation.但是,当我向它抛出多个文件时,最后上传的文件会覆盖每个操作的 static 变量。 I'm new to spring and I have read each request is handled in a different container.我是 spring 的新手,我已经阅读过每个请求都在不同的容器中处理。 I'm not able to understand how one 'container' is creating ruckus in another 'container'?我无法理解一个“容器”如何在另一个“容器”中制造骚动?

Your understanding about "requests in different containers" is completely erroneous.您对“不同容器中的请求”的理解是完全错误的。 Spring doesn't handle sessions directly; Spring 不直接处理会话; the embedded Servlet engine (usually Tomcat) does, unless you specifically include something like Spring Session that positively takes over the handling.嵌入式 Servlet 引擎(通常是 Tomcat)会这样做,除非您特别包含 Spring Session 之类的东西,它肯定会接管处理。

In either case, though, this has nothing to do with sessions and everything to do with shared state (whether static or via a singleton Spring bean). In either case, though, this has nothing to do with sessions and everything to do with shared state (whether static or via a singleton Spring bean). It would be absurdly expensive to spin up a completely new copy of the application for each request, and even if you did you'd still lose your state between requests.为每个请求启动一个全新的应用程序副本将会非常昂贵,即使您这样做了,您仍然会在请求之间丢失您的 state。 You need to (less ideal) store the variable in a session attribute or (best) return some sort of ID with an HTTP 202 Accepted status and keep an internal table so that it can be checked on later.您需要(不太理想)将变量存储在session 属性中,或者(最好)返回某种带有 HTTP 202 Accepted 状态的 ID,并保留一个内部表,以便以后检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM