Skip to content

bean scopes

Eric F. Alsheimer edited this page Jan 12, 2018 · 1 revision

Bean Scopes

Scope

  • Singleton - One bean per IoC container. This is best for stateless beans (this is the default)
  • Prototype - Any instances without limit. This is best for stateful beans
  • Request - Limits scope to lifecycle of single HTTP request
  • Session - Limits scope to lifecycle of a single HTTP session (user across multiple requests) [web-aware]
  • Global - Limits to global HTTP session (usually with portlets) [web-aware]

Scoped Proxy

To inject a HTTP request-scoped bean (or session-scoped bean) into another bean, you must inject an AOP scoped proxy instead. What the proxy exposes is the same public interface as the scoped object, and it is smart enough to delegate calls to that proxy object onto the HTTP request- or session-scoped bean. This is only necessary for request- and session-scopes and will error on prototype or singleton scopes.

Session vs Request Scope

The easiest way to explain this is when you have a request scope, all values are initialized and no state is being retained. The session scope is not re-initialized (within the user experience) and the state is retained.

Clone this wiki locally