@Controller public
class acontroller{
@Autowire
httpservletrequest request;
@RequestMapping ("/test") public result
test () {
System.out.println (request.tostring ());
Request.getheader ("UID");
}
For example, the above code,
After I use Autowire to inject request, I use request directly in the Controller method,
Since the controller default is a single example, I was wondering if there would be a thread security problem.
Because I output the hashcode of the request, the hashcode is the same every time.
Then whether the request will overwrite the current request causes the information to be distorted.
····························· Supplemental
1, I want to use HttpServletRequest in every method of controller, then every time in the method to declare I feel more trouble. I don't know how we can solve the problem.
2. The reason I write this is that I want to use the request as a member variable of the parent class by inheriting a parent class, which can be used directly in the method.
3, I through the downstairs Fork Brother Way (before that is written)
Public result test (HttpServletRequest request) {
System.out.println (request.tostring ());
}
It is also constantly accessed, or accessed with different clients. Discover that the hashcode of each request that is printed is actually the same, why.
This writing is thread safe for the following reasons: the request injected in controller is the JDK dynamic proxy object, instance of Objectfactorydelegatinginvocationhandler. When we call the member domain request method, we actually invoke the Objectfactory GetObject () Object. The objectfactory here is requestobjectfactory. The GetObject of Requestobjectfactory is actually taken from the threadlocal of Requestcontextholder. Requests have just entered the SPRINGMVC Dispatcherservlet will be the request related objects set to Requestcontextholder threadlocal.
Reference: Inject the request member field into the SPRINGMVC controller