Document autowiring of ServletRequest and other scoped proxy beans provided by WebApplicationContext #26201
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: documentation
A documentation task
Milestone
I recently stumbled upon the fact that it's possible to autowire
HttpServletRequest
into singleton-scoped Spring beans, despite the object being specific to a particular web request, and not globally applicable. From what I've been able to determine, this is a proxy object registered byWebApplicationContextUtils.registerWebApplicationScopes(beanFactory, servletContext)
. It is added byspring-web
(and/or byspring-boot
) to the bean factory byAbstractRefreshableWebApplicationContext
(or its subclass overrides).Looking at the source for
WebApplicationContextUtils
, it looks like the following objects are provided in this manner:ServletRequest
ServletResponse
HttpSession
WebRequest
FacesContext
(if JSF present)ExternalContext
(if JSF present)While handy, this behavior was somewhat surprising, since I didn't expect to be able to autowire in something that was inherently tied to a specific web request to a singleton bean that is shared across requests.
From what I have been able to tell (as discussed in this Stack Overflow question), this feature is not documented anywhere obvious. The only documentation I've seen is the issue that added it (#9808 (comment)) and the release notes when it was added:
It would be helpful if this feature were documented. In particular, it would be nice to have documentation that these beans are available for autowiring and the situations when they are not available (e.g. when executing outside of the scope of a request).
The text was updated successfully, but these errors were encountered: