Will there be thread safety issues when injecting requests into controllers in Springmvc?


Will there be thread safety issues when injecting requests into controllers in Springmvc?


@Controller
public class AController{

    @Autowire
    HttpServletRequest request;

    @RequestMapping("/test")
    public Result test(){
        System.out.println(request.toString());
        request.getHeader("uid");
    }
}



1. The HttpServletRequest injected by @Autowire is just an "empty shell", and the real object is obtained from the thread when it is accessed.
2. The injected request object is an instance of the HttpServletRequest class. After repeating the http request many times, it is found that the id of this object is always the same
. 3. But when we get the value in the setting, we will get the current thread association through an interceptor SpringExtUtil The "ServletRequestAttributes" object is obtained.
4. So this will not cause thread safety problems due to different requests changing the value plane inside.
5. It can be understood that each requested HttpServletRequest is an instantiation associated with a thread.



Reference text: https://my.oschina.net/sluggarddd/blog/678603?fromerr=XhvpvVTi
Reference text: http://www.th7.cn/Program/java/201503/420240.shtml

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988707&siteId=291194637