简体   繁体   English

在Spring控制器中自动装配HttpServletRequest

[英]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变量的支持下注册ServletRequestServletResponseHttpSessionWebRequest类型的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.

相关问题 Spring MVC:自动在另一个控制器中布线 - Spring MVC : autowiring a controller in another controller 在Spring控制器中从HttpServletRequest读取对象 - Read Object from HttpServletRequest in Spring controller 弹簧自动接线 - Spring autowiring 春季启动引发异常时在我的控制器中自动装配 Feign 客户端 - Autowiring a Feign client in my controller when spring launching throws exception 在 Spring Boot 中的控制器中自动装配通用(T)服务 - Autowiring Generic(T) Service in Controller within Spring Boot 如何通过自动装配服务在 spring-boot 上测试控制器层? - How to test controller layer on spring-boot by autowiring service? Spring MVC不会在HTTP POST中自动装配,而是在同一控制器上的Ajax中装配 - Spring MVC not autowiring in HTTP POST, but doing in Ajax on the same controller Spring Autowiring Service在我的Controller中不起作用 - Spring Autowiring Service doesn't work in my Controller Spring @Controller、@Service 和@Component 中@Autowire HttpServletRequest 请求的并发问题 - Concurrency issues on @Autowire HttpServletRequest request in Spring @Controller,@Service and @Component 自动装配HttpSession提供与HttpServletRequest不同的对象 - Autowiring HttpSession give different object than HttpServletRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM