java Spring 控制器是线程安全的吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14145761/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:14:25  来源:igfitidea点击:

Are Spring Controllers Thread-Safe

javaspring

提问by EdgeCase

I came upon this Controller example, and am wondering if it is thread-safe? I am wondering specifically about the gson instance variable.

我遇到了这个控制器示例,想知道它是否是线程安全的?我特别想知道 gson 实例变量。

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class TestController {

    private final Gson gson = new Gson();

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public void test(HttpServletResponse res, HttpServletRequest req) throws IOException {
        HashMap<String, String> hm = new HashMap();
        res.getWriter().print(gson.toJson(results));
        res.getWriter().close();
    }
}

回答by digitaljoel

To answer the question in the title, "Are Spring Controllers Thread-Safe", Spring Controllers are singletons, therefore they SHOULD be implemented in a thread safe manner but it's up to you to ensure that your implementation is ACTUALLY thread safe.

要回答标题中的问题,“Spring 控制器是线程安全的吗”,Spring 控制器是单例,因此它们应该以线程安全的方式实现,但由您来确保您的实现实际上是线程安全的。

In the code you post you should be fine since as others have pointed out, GSON is thread safe.

在您发布的代码中,您应该没问题,因为正如其他人指出的那样,GSON 是线程安全的。

回答by parsifal

Assuming that this controller is created as a normal Spring "singleton" bean, the answer is no.

假设这个控制器是作为一个普通的 Spring“单例”bean 创建的,答案是否定的

You could create the controller as a prototypebean, in which case a new instance would be created for each request. A better idea, if you want to do this, is to define your bean's scope as request.

您可以将控制器创建为原型bean,在这种情况下,将为每个请求创建一个新实例。如果您想这样做,一个更好的主意是将 bean 的范围定义为request.

However, I question the reason for anycontroller object to have member variables, aside from the possibility of incorrectly scoping the bean. It's an indication that the controller is trying to do too much work, and that some of that work should be offloaded to a service or helper class. The only things that an MVC controller should do arepass request data on to a service layer, and retrieve data to be displayed by a view.

但是,我质疑任何控制器对象具有成员变量的原因,除了可能错误地确定 bean 的范围之外。这表明控制器正在尝试做太多工作,其中一些工作应该卸载到服务或帮助程序类。MVC 控制器应该做的唯一事情是将请求数据传递到服务层,并检索要由视图显示的数据。

回答by ramsinb

Gsonis definitely thread safe and was made this way back in 2008, so as long as your version is post that then it should be fine. I see no thread safety issues with your code. Although I would make the instance of Gsonstatic.

Gson绝对是线程安全的,并且是在 2008 年以这种方式制作的,所以只要你的版本是发布的,那么它应该没问题。我发现您的代码没有线程安全问题。虽然我会做Gson静态的实例。

回答by Subin Sebastian

Just like servlets controller request handler methods are also not thread safe. Ie a multiple request to /testmay make many threads execute test method.

就像 servlet 控制器请求处理程序方法也不是线程安全的。即多个请求/test可能使多个线程执行测试方法。

In your example you dont have to worry about thread safety as gson.toJson(results)is the only operation on gson and seems like it wont alter the state of that object.

在您的示例中,您不必担心线程安全,因为这gson.toJson(results)是对 gson 的唯一操作,并且似乎它不会改变该对象的状态。