必须Spring MVC类是线程安全的 [英] Must Spring MVC Classes be Thread-Safe

查看:103
本文介绍了必须Spring MVC类是线程安全的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你使用Spring MVC,你的组件类必须是( @Controller @Service @Repository )是线程安全的吗?

If you use Spring MVC, must your component classes (@Controller, @Service, @Repository) be thread safe?

也就是说,如果我有 @RequestMapping @Controller 中的$ c>方法,是否可以通过多个线程同时为同一控制器对象调用该方法?

That is, if I have a @RequestMapping method in my @Controller, could that method be called simultaneously for the same controller object by more than one thread?

(这有之前已被问过的问题,但没有回答。)

(This has sort-of been asked before, but not answered as such).

推荐答案

给定

@Controller
public class MyController {
    @RequestMapping(value = "/index")
    public String respond() {
        return "index";
    }
}

Spring将创建一个 myController的。这是因为Spring解析了你的配置,< mvc:annotation-driven> ,看到 @Controller (这就像 @Component )并实例化带注释的类。因为它也看到 @RequestMapping ,它会为它生成 HandlerMapping ,请参阅 docs here

Spring will create an instance of MyController. This is because Spring parses your configuration, <mvc:annotation-driven>, sees @Controller (which is like @Component) and instantiates the annotated class. Because it sees @RequestMapping as well, it generates a HandlerMapping for it, see the docs here.

将通过 HandlerMapping将 DispatcherServlet 收到的任何HTTP请求分派到此控制器实例之前注册,通过该实例上的java反射调用 respond()

Any HTTP requests the DispatcherServlet receives will be dispatched to this controller instance through the HandlerMapping registered before, calling respond() through java reflection on that instance.

如果你有像

@Controller
public class MyController {
    private int count = 0;
    @RequestMapping(value = "/index")
    public String respond() {
        count++;
        return "index";
    }
}

count 会有危险,因为它可能会被许多线程修改,对它的更改可能会丢失。

count would be a hazard, because it might be modified by many threads and changes to it might be lost.

您需要了解Servlet容器的工作原理。容器实例化Spring MVC的一个实例 DispatcherServlet 。容器还管理一个用于响应连接的线程池,即。 HTTP请求。当这样的请求到达时,容器从池中选择一个Thread,并在该Thread中,在 DispatcherServlet上执行 service()方法调度到Spring为您注册的正确的 @Controller 实例(来自您的配置)。

You need to understand how Servlet containers work. The container instantiates one instance of your Spring MVC DispatcherServlet. The container also manages a pool of Threads which it uses to respond to connections, ie. HTTP requests. When such a request arrives, the container picks a Thread from the pool and, within that Thread, executes the service() method on the DispatcherServlet which dispatches to the correct @Controller instance that Spring registered for you (from your configuration).

所以,是的,Spring MVC类必须是线程安全的。您可以通过为类实例字段使用不同的范围或仅使用局部变量来执行此操作。如果做不到这一点,您需要在代码中的关键部分周围添加适当的同步。

So YES, Spring MVC classes must be thread safe. You can do this by playing with different scopes for your class instance fields or just having local variables instead. Failing that, you'll need to add appropriate synchronization around critical sections in your code.

这篇关于必须Spring MVC类是线程安全的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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