Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document autowiring of ServletRequest and other scoped proxy beans provided by WebApplicationContext #26201

Closed
mjustin opened this issue Dec 3, 2020 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: documentation A documentation task
Milestone

Comments

@mjustin
Copy link

mjustin commented Dec 3, 2020

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 by WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, servletContext). It is added by spring-web (and/or by spring-boot) to the bean factory by AbstractRefreshableWebApplicationContext (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:

As of Spring 3.0 RC1, we generally inject proxies for request and session objects now. This has the advantage of working in singleton beans and serializable beans as well, and to always obtain the current session handle - even in case of invalidation between requests or between multiple calls within the same request.

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).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 3, 2020
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 10, 2021
@snicoll snicoll changed the title Document autowiring of ServletRequest and other scoped proxy beans provided by WebApplicationContextUtils.registerWebApplicationScopes Document autowiring of ServletRequest and other scoped proxy beans provided by WebApplicationContext Dec 1, 2023
@snicoll snicoll added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 1, 2023
@snicoll snicoll added this to the General Backlog milestone Dec 1, 2023
@jhoeller jhoeller added in: web Issues in web modules (web, webmvc, webflux, websocket) and removed in: core Issues in core modules (aop, beans, core, context, expression) labels Dec 23, 2023
@jhoeller jhoeller self-assigned this Dec 27, 2023
@jhoeller jhoeller modified the milestones: General Backlog, 6.1.3 Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

5 participants