@SessionScope is a Spring annotation that gives a bean one instance per HTTP session. Each user session gets its own copy, which lives until the session expires or is invalidated. It is a shortcut for @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS), so the bean can be injected safely into singletons.
It only works in a web-aware application context, for example a Spring MVC or Spring Boot web application. In a plain console application there is no HTTP session and Spring throws an exception when the bean is requested.
The annotation lives in org.springframework.web.context.annotation and is a composed annotation:
Spring creates the bean the first time the current session asks for it and stores it as an attribute of that HttpSession. When the session ends, the bean is destroyed and its @PreDestroy method runs.
| Scope | Annotation | One instance per |
|---|---|---|
singleton | default | Application context |
prototype | @Scope("prototype") | Injection or lookup |
request | @RequestScope | HTTP request |
session | @SessionScope | HTTP session |
application | @ApplicationScope | Servlet context |
A shopping cart is the classic use:
Two details matter. The class implements Serializable because servlet containers can persist or replicate sessions, and the methods are synchronized because an HttpSession can be touched by parallel requests from the same browser.
Controllers and services are singletons, but the cart must differ per user. Because @SessionScope applies a scoped proxy, you can inject the cart by constructor like any other bean:
On every call the proxy looks up the real ShoppingCart of the current session. Without the proxy, the singleton would receive one instance at startup, and creating it then would fail because no session exists. That is why the ScopedProxyMode.TARGET_CLASS default is useful. It uses a CGLIB subclass, so the class must not be final, and the proxied methods must not be final either.
A test with MockMvc shows the isolation:
Session beans keep memory on the server for every active user, and they tie requests to a particular instance unless sessions are shared. Prefer these alternatives when you can:
@RequestScope for data that only lives for one request.@SessionScope beans then live in the shared store, so they must be serializable.Do not keep large objects in session beans, and never store secrets longer than needed.
IllegalStateException: No thread-bound request found: the bean was requested outside a web request, for example from a background thread or at startup. Inject it only in code that runs during a request.@Bean method declares a different scope.jakarta.servlet, which only matters if your bean touches HttpSession directly.@SessionScope also sets the scoped proxy mode to TARGET_CLASS. With plain @Scope("session") you must add proxyMode yourself before injecting into singletons.
Not automatically. Several requests of the same session can run at once, so protect mutable state.
Until the HTTP session expires or is invalidated, for example on logout. In Spring Boot the default timeout is 30 minutes and is set with server.servlet.session.timeout.
No. WebFlux has no HttpSession scope. Use WebSession or an external store.
@SessionScope gives each HTTP session its own bean instance with a scoped proxy, which makes per-user state such as a cart easy to model. Make the bean serializable and thread-safe, inject it by constructor, and move to a shared store or stateless design when you scale beyond one server.