Spring:如何将HttpServletRequest注入请求范围的bean?

时间:2022-09-17 11:02:53

I'm trying to set up a request-scoped bean in Spring.

我正在尝试在Spring中设置一个请求范围的bean。

I've successfully set it up so the bean is created once per request. Now, it needs to access the HttpServletRequest object.

我已成功设置它,因此每个请求都会创建一次bean。现在,它需要访问HttpServletRequest对象。

Since the bean is created once per request, I figure the container can easily inject the request object in my bean. How can I do that ?

由于每个请求创建一次bean,我认为容器可以很容易地在我的bean中注入请求对象。我怎样才能做到这一点 ?

2 个解决方案

#1


81  

Request-scoped beans can be autowired with the request object.

请求范围的bean可以与请求对象一起自动装配。

private @Autowired HttpServletRequest request;

#2


109  

Spring exposes the current HttpServletRequest object (as well as the current HttpSession object) through a wrapper object of type ServletRequestAttributes. This wrapper object is bound to ThreadLocal and is obtained by calling the static method RequestContextHolder.currentRequestAttributes().

Spring通过ServletRequestAttributes类型的包装器对象公开当前的HttpServletRequest对象(以及当前的HttpSession对象)。此包装器对象绑定到ThreadLocal,并通过调用静态方法RequestContextHolder.currentRequestAttributes()获得。

ServletRequestAttributes provides the method getRequest() to get the current request, getSession() to get the current session and other methods to get the attributes stored in both the scopes. The following code, though a bit ugly, should get you the current request object anywhere in the application:

ServletRequestAttributes提供方法getRequest()来获取当前请求,getSession()获取当前会话以及其他方法来获取存储在两个范围中的属性。以下代码虽然有点难看,但应该在应用程序的任何位置获取当前请求对象:

HttpServletRequest curRequest = 
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();

Note that the RequestContextHolder.currentRequestAttributes() method returns an interface and needs to be typecasted to ServletRequestAttributes that implements the interface.

请注意,RequestContextHolder.currentRequestAttributes()方法返回一个接口,需要将其类型化为实现该接口的ServletRequestAttributes。


Spring Javadoc: RequestContextHolder | ServletRequestAttributes

Spring Javadoc:RequestContextHolder | ServletRequestAttributes

#1


81  

Request-scoped beans can be autowired with the request object.

请求范围的bean可以与请求对象一起自动装配。

private @Autowired HttpServletRequest request;

#2


109  

Spring exposes the current HttpServletRequest object (as well as the current HttpSession object) through a wrapper object of type ServletRequestAttributes. This wrapper object is bound to ThreadLocal and is obtained by calling the static method RequestContextHolder.currentRequestAttributes().

Spring通过ServletRequestAttributes类型的包装器对象公开当前的HttpServletRequest对象(以及当前的HttpSession对象)。此包装器对象绑定到ThreadLocal,并通过调用静态方法RequestContextHolder.currentRequestAttributes()获得。

ServletRequestAttributes provides the method getRequest() to get the current request, getSession() to get the current session and other methods to get the attributes stored in both the scopes. The following code, though a bit ugly, should get you the current request object anywhere in the application:

ServletRequestAttributes提供方法getRequest()来获取当前请求,getSession()获取当前会话以及其他方法来获取存储在两个范围中的属性。以下代码虽然有点难看,但应该在应用程序的任何位置获取当前请求对象:

HttpServletRequest curRequest = 
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();

Note that the RequestContextHolder.currentRequestAttributes() method returns an interface and needs to be typecasted to ServletRequestAttributes that implements the interface.

请注意,RequestContextHolder.currentRequestAttributes()方法返回一个接口,需要将其类型化为实现该接口的ServletRequestAttributes。


Spring Javadoc: RequestContextHolder | ServletRequestAttributes

Spring Javadoc:RequestContextHolder | ServletRequestAttributes