Spring MVC如何处理多个用户 [英] How does Spring MVC handle multiple users

查看:185
本文介绍了Spring MVC如何处理多个用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用弹簧的时间已经超过6个月了.我无法理解与以下情况相关的这种潜在机制.

I am using spring from more than 6 months. I am not able to understand this underlying mechanism related to the below scenario.

我有一个Spring Web应用程序.现在,我将模型自动连接到控制器中.基于url匹配,它将调用相应的方法.我所有的方法都是单例.

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可以并行运行它们并为他们提供结果.我不明白它怎么能做到这一点.我的意思是,由于bean是单例的,因此必须等到不使用bean或覆盖bean中的数据.但是弹簧工作正常.有人可以用类似的方式解释这种行为.

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.

为清楚地说明我的问题,下面是一段代码:

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;
}

有人可以告诉我,当两个客户端打开应用程序时,当我使用@autowired时,将生成两个单独的模型.如果所有客户仅存在一个模型bean,则说来自客户1的请求进入,我花了30秒钟才能得到结果.现在,如果第二个客户端在3秒钟内发送请求,那么第一个客户端的请求会被覆盖吗?

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?

我觉得我很困惑.有人可以澄清这种魔术是如何发生的吗?

I think I am getting confused. Can some one clarify how this magic is happening?

谢谢

推荐答案

每个Web请求都会生成一个新线程,如

Every web request generate a new thread as explained in this thread.

Spring管理不同的范围(原型,请求,会话,单例).如果两个同时请求访问一个单例bean,则该bean必须是无状态的(或至少是同步的,以避免出现问题).如果您在范围请求中访问bean,则将为每个请求生成一个新实例. Spring为您管理此操作,但您必须小心并为您的bean使用正确的作用域.通常,您的控制器是单例,但是AppModel必须在范围request范围内,否则您将遇到两个同时请求的问题. 此线程也可以为您提供帮助.

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 AppModel has to be of scope request, otherwise you will have problems with two simultaneous requests. This thread could also help you.

关于您的最后一个问题魔术是如何发生的?",答案是方面/代理". Spring创建代理类.您可以想象Spring将为您的AppModel类创建一个代理.一旦您尝试在控制器中访问它,Spring就会将方法调用转发到 right 实例.

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 AppModel class. As soon as you try to access it in the controller, Spring forwards the method call to the right instance.

这篇关于Spring MVC如何处理多个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆