【发布时间】:2017-01-30 10:19:19
【问题描述】:
这对我来说很奇怪。 我有以下代码:
@Controller
public class MyTestController {
@Autowired
private HttpServletRequest request;
@RequestMapping("/print/")
public String PrintInfo() throws Exception {
Thread.sleep(5000);
System.out.println("name:" +request.getParameter("name"));
System.out.println("hashcode:" +request.hashCode());
return "testpage";
}
}
我同时访问了以下网址:
http://127.0.0.1:8080/MyApp/print/?name=tom
http://127.0.0.1:8080/MyApp/print/?name=mike
打印出来了:
name:tom
hashcode:2006506443
name:mike
hashcode:2006506443
MyTestContorller 是单例的。请求的hashcode相同,表示不同的请求具有相同的实例。
但是为什么request.getParameter("name") 会给我正确的结果?
也许getParameter方法只是一个需要从其他对象读取数据的方法?
我不知道,这让我很困惑。谁能解释一下?
【问题讨论】:
-
打印您的请求的类...它可能是一个 JDK 代理,它能够从正确的范围中提取依赖项(由
RequestContextHolder上的本地线程保留)。我认为 Spring 文档中有一章专门讨论这个...... -
魔法。我说这是一种邪恶的魔法:)这个聪明的把戏破坏了我们对物体的常识。
标签: java spring multithreading spring-mvc servlets