[英]Autowiring HttpServletRequest in Spring controller
Let's say I have a Spring controller. 假设我有一个Spring控制器。
@RequestMappin("/path")
public MyController {
}
As stated, default scope of the controller is Singleton. 如上所述,控制器的默认范围是Singleton。 I know that I can autowire request in REQUEST scope beans, however, if I try to autowire request, so that
我知道我可以在REQUEST范围bean中自动发送请求,但是,如果我尝试自动发送请求,那么
@RequestMappin("/path")
public MyController {
@Autowired
private HttpServletRequest request;
}
It still works, and for each request I get appropriate request object. 它仍然有效,并为每个请求我得到适当的请求对象。 Does it mean that autowire works regardless the scope is request or not?
这是否意味着无论范围是否请求,autowire都能正常工作?
if it works that means spring doesn't inject exactly http request but a proxy. 如果它工作,这意味着spring不会完全注入http请求而是代理。 the proxy delegates calls to current http request
代理委托调用当前的http请求
When a spring web based application bootstraps, it will register the bean of type ServletRequest
, ServletResponse
, HttpSession
, WebRequest
with the support of ThreadLocal variables. 当基于spring web的应用程序引导时,它将在ThreadLocal变量的支持下注册
ServletRequest
, ServletResponse
, HttpSession
, WebRequest
类型的bean。 So whenever you request one kind of above four, the actual value will be the actual stored ThreadLocal variable that are bound to the current thread. 因此,每当您请求一种以上四种时,实际值将是绑定到当前线程的实际存储的ThreadLocal变量。
You can find the details implementation mechanisms of @Autowired HttpServletRequest at @Autowired HttpServletRequest 您可以在@Autowired HttpServletRequest中找到@Autowired HttpServletRequest的详细实现机制
You can get HttpServletRequest
object in each webservice method. 您可以在每个Web服务方法中获取
HttpServletRequest
对象。 Such as: 如:
@RequestMapping("/method")
public void method(HttpServletRequest req) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.