简体   繁体   English

春豆的并发访问安全

[英]Concurrent Access Safety at Spring Beans

I have current method: 我有当前方法:

private int getStatusCode(String path) throws IOException {
  HttpClient client = HttpClients.createDefault();
  HttpGet method = new HttpGet(path);
  HttpResponse httpResponse = client.execute(method);
  return httpResponse.getStatusLine().getStatusCode();        
}

I want to put it in a Spring @Service class. 我想将其放在Spring @Service类中。 Then I want to use that class as @Autowired . 然后我想将该类用作@Autowired I know that Spring beans are singleton so there will be only one instance of my autowired class. 我知道Spring bean是单例的,因此我的autowired类只有一个实例。 I have different @Controller classes which use that service. 我有使用该服务的不同@Controller类。 Does concurrent accesses makes a problem at my case? 就我而言,并发访问是否会引起问题? For example overriding path from another request? 例如,覆盖另一个请求的路径?

Does concurrent accesses makes a problem at my case? 就我而言,并发访问是否会引起问题?

Although it is a singleton, tt doesn't cause any concurrency issues because the method getStatusCode uses only the 尽管它是单例,但是tt不会引起任何并发问题,因为方法getStatusCode仅使用

  • objects that are passed to the method as parameters(ex: String path) 作为参数传递给方法的对象(例如:字符串路径)
  • Objects that are created with in the method. 在方法中使用创建的对象。

For example overriding path from another request? 例如,覆盖另一个请求的路径?

Not an issue because each method call will be from different threads and parameters to method (like path variable) are stored on the stack which is distinct for each thread. 这不是问题,因为每个方法调用都将来自不同的线程,并且方法的参数(如路径变量)存储在堆栈中,每个线程都不同。

As an real time example we have take look at the Servlet. 作为一个实时示例,我们来看一下Servlet。 Only one instance of the servlet is created by the servlet container irrespective of number of requests. 不管请求的数量如何,Servlet容器仅创建Servlet的一个实例。 All the request threads access this single servlet instance's (doGet(...) or doPost(...)) methods by passing their own HttpServletRequest and HttpServletResponse objects to the above methods and it doesn't result mixing-up the requests/responses ever. 通过将它们自己的HttpServletRequestHttpServletResponse对象传递给上述方法,所有请求线程都将访问该单个servlet实例的(doGet(...) or doPost(...))方法,并且不会导致请求/响应混淆曾经。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM