@Controller
public class AController{
@Autowire
HttpServletRequest request;
@RequestMapping("/test")
public Result test(){
System.out.println(request.toString());
request.getHeader("uid");
}
}
例如上述代码,
我使用Autowire
注入request
后,直接在controller
的方法中使用request
,
由于controller
默认是单例的,我在想是否会有线程安全问题。
因为我输出了request
的hashcode
发现每次请求hashcode
都是一样的。
那么后面的request
是否会覆盖当前request
导致信息失真?
·····························补充··························
1、我想在controller
的每个方法里都使用HttpServletRequest
,那么每次在方法里都要声明我觉得就比较麻烦了?不知道大家怎么解决这个问题?
2、我这样写的原因是,我想通过继承一个父类的方式,让request作为父类的一个成员变量,这样可以在方法里直接使用。
3、我通过楼下叉叉哥的方式(之前就是这样写的)
public Result test(HttpServletRequest request){
System.out.println(request.toString());
}
同样不断访问,或者用不同客户端访问。发现打印出来的每个请求的request的hashcode居然也是相同的,为什么?
Thank you everyone for your answers
After my testing and exploration.
The conclusion is that
using
@autowire
injecting HttpServletRequest is thread-safe.I wrote a blog about the specific verification process
If you are interested, you can read it. If there is anything wrong, please point it out.
Thank you again to the programmers upstairs who enthusiastically answered my questions
Click me for blog address
ps: The above discussion is all about Controller in singleton mode
···
First of all, in principle, Request cannot be defined as a member of Controller, because the life cycles of the two are completely disconnected. This will only cause the Controller to be unable to call the correct Request object.
Secondly, @Autowire is assigned once, and there are countless Request objects, so if you write it like this, Spring will not know what to do. Because there is no Request object when the application starts, this should cause the startup to fail.
Um, I'm showing off my shame, just focusing on solving minor issues
request
每次相同的问题了,忘了主题了。加
@ModelAttribute
It is truethat there will be thread safety issues!...
If you want to use object properties and handle thread safety, there is a method that is very simple, but particularly rough. Directly use
@Scope("prototype")
to let Spring MVC request each time A new entity class is generated...@Scope("prototype")
让Spring MVC每次请求都生成一个新的实体类...但Spring MVC为什么默认要单例?自然是因为单例的性能和开销的优点,
而写
@Scope("prototype")
But why does Spring MVC require a singleton by default? Naturally, because of the performance and cost advantages of singletons, writing@Scope("prototype")
means giving up the advantages of singletons.So this is indeed a way, but it’s not good. It’s embarrassing for everyone, I’m sorry.
1. After Autowire injects the request, there will be security issues when using instance variables.
It seems troublesome to write parameters in the method, so you can:2. The request will be overwritten. 3. The hashcode is still the same object.
This is the first time I have seen this way of writing. Why not write it like this?
In fact, in most cases, request does not need to be passed as a parameter. For example, if you want to get the uid in the request header, you can write like this:
If you write it that way, thread safety issues will definitely occur, because each controller in spring is singleton by default, so your request will be shared by other threads, so it is recommended that you write it in the same way as @ChaCha哥.
There will be. In Servlet, attribute parameters are written in the method so that they will not be shared
If you write like this, there will definitely be thread safety issues, because the controller is a singleton by default, which means that multiple requests will share an HttpServletRequest object. If you add @Scope("prototype"), there will be no problem
There will be thread safety issues, I suggest you read this article http://www.xuebuyuan.com/1628190.html
Any class instance variable has potential thread safety risks. The code needs to ensure that the instance variable is ok under multi-threading, or ensure that the class is accessed by only one thread at the same time.
The problem of multi-threading in your example is inevitable. You should follow the Spring tutorial and write it in the standard way. Get things right and understand them first, and then move on to complicated ones.