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

查看:58
本文介绍了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 网络应用程序.现在我在控制器中自动连接模型.基于 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 就会将方法调用转发到正确的 实例.

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天全站免登陆