Spring MVC 如何处理多用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17235794/
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
How does Spring MVC handle multiple users
提问by javaMan
I am using spring from more than 6 months. I am not able to understand this underlying mechanism related to the below scenario.
我正在使用超过 6 个月的弹簧。我无法理解与以下场景相关的这种底层机制。
I have a spring web app. Now I autowired the model in controller. Based on url matching it calls respective method. all my methods are singleton.
我有一个 spring 网络应用程序。现在我在控制器中自动连接模型。基于 url 匹配,它调用相应的方法。我所有的方法都是单身。
Now when two users are opening app at same time spring is able to run them parallelly and give results to them. I didnt understand how can it do this. i mean as the bean is singleton it has to either the wait till the bean is not used or overwrite the data in the bean. But spring is working correctly. Can someone explain this behaviour with some analogy.
现在,当两个用户同时打开应用程序时,spring 可以并行运行它们并为它们提供结果。我不明白它怎么能做到这一点。我的意思是当 bean 是单例时,它必须等待直到 bean 未被使用或覆盖 bean 中的数据。但弹簧工作正常。有人可以用一些类比来解释这种行为。
To explain my question clearly below is a piece of code:
为了清楚地解释我的问题,下面是一段代码:
My default controller is simple one:
我的默认控制器很简单:
@Autowired
private AppModel aModel;
public AppModel getModel(){
return aModel;
}
public void setModel(AppModel aModel){
this.aModel = aModel;
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView defaultGetter(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView(getViewName());
mav.addObject("model", aModel);
Runtime.getRuntime().gc();
return mav;
}
Also can some one tell me when two clients open the app will two seperate models get generated when i use @autowired . If only one model bean exists for all clients then say the request from client 1 came in and it take me 30 sec to get results back. Now if second client sends request in 3rd sec then will the first clients request gets overwritten?
也有人可以告诉我,当两个客户端打开应用程序时,当我使用 @autowired 时会生成两个单独的模型。如果所有客户端只存在一个模型 bean,那么说来自客户端 1 的请求进来了,我需要 30 秒才能返回结果。现在,如果第二个客户端在第 3 秒发送请求,那么第一个客户端请求会被覆盖吗?
I think I am getting confused. Can some one clarify how this magic is happening?
我想我越来越糊涂了。有人可以澄清这种魔法是如何发生的吗?
Thanks
谢谢
回答by LaurentG
Every web request generate a new thread as explained in this thread.
每个 Web 请求都会生成一个新线程,如本线程中所述。
Spring manages different scopes (prototype, request, session, singleton). If two simultaneous requests access a singleton bean, then the bean must be stateless (or at least synchronized to avoid problems). If you access a bean in scope request, then a new instance will be generated per request. Spring manages this for you but you have to be careful and use the correct scope for your beans. Typically, your controller is a singleton but the AppModelhas to be of scope request, otherwise you will have problems with two simultaneous requests. This thread could also help you.
Spring 管理不同的范围(原型、请求、会话、单例)。如果两个同时请求访问一个单例 bean,那么 bean 必须是无状态的(或者至少是同步的以避免出现问题)。如果您在请求范围内访问 bean,那么每个请求都会生成一个新实例。Spring 为您管理这个,但您必须小心并为您的 bean 使用正确的范围。通常,您的控制器是一个单例,但AppModel必须是 scope request,否则您将遇到两个同时请求的问题。这个线程也可以帮助你。
About your last question "how this magic is happening?", the answer is "aspect/proxy". Spring create proxy classes. You can imagine that Spring will create a proxy to your AppModelclass. As soon as you try to access it in the controller, Spring forwards the method call to the rightinstance.
关于你的最后一个问题“这种魔法是如何发生的?”,答案是“方面/代理”。Spring 创建代理类。您可以想象 Spring 将为您的AppModel类创建一个代理。一旦您尝试在控制器中访问它,Spring 就会将方法调用转发到正确的实例。

