The @SessionScope annotation tells Spring to create one instance of a bean per HTTP session. Every request from the same user gets the same instance, a different user gets a different one, and the bean is discarded when the session expires or is invalidated. It is a shortcut for @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) and lives in org.springframework.web.context.annotation.
Use it for small pieces of per-user state such as a shopping cart or wizard progress in a server-rendered web application. For APIs that should stay stateless, prefer tokens or a shared store instead.
Spring normally creates singleton beans: one instance for the whole application. Web-aware scopes tie the bean lifetime to a web object instead. @SessionScope binds it to the HttpSession:
invalidate() is called.The annotation has been available since Spring 4.3. It only works in a web application context (Spring MVC or WebFlux with a web session, including Spring Boot web apps). In a plain non-web context you get an IllegalStateException about no thread-bound request.
Each user session gets its own ShoppingCart. The class implements Serializable because servlet containers may persist sessions to disk or replicate them to other nodes. The methods are synchronized because a browser can fire several requests for the same session in parallel, and the bean is shared between those threads.
Controllers and services are singletons, yet they need the cart of the current user. This works because @SessionScope uses a scoped proxy by default (TARGET_CLASS). Spring injects a proxy once, and on every method call the proxy looks up the real bean in the current session.
Call POST /cart/book-42?qty=2 twice with the same session cookie and the quantity accumulates. A second browser, with a different JSESSIONID, sees an empty cart.
Because the proxy is a class-based proxy, the bean class must not be final and needs a constructor Spring can call. Session beans also cannot be looked up outside a request thread, for example in a scheduled job.
| Scope | Annotation | Lifetime | Typical use |
|---|---|---|---|
| singleton | default | Whole application | Services, repositories |
| prototype | @Scope("prototype") | New instance per injection or lookup | Stateful helpers |
| request | @RequestScope | One HTTP request | Request context, correlation data |
| session | @SessionScope | One HTTP session | Cart, user preferences, wizard state |
| application | @ApplicationScope | One ServletContext | Shared web-wide state |
If you only need data for a single request, @RequestScope is cheaper because nothing stays in memory between requests.
Serializable and its fields serializable if you use session persistence or Spring Session with Redis or a database.jakarta.servlet; the scope annotation itself is unchanged.To make sessions survive restarts or span several instances, add Spring Session and configure a store. Session-scoped beans then work across nodes without code changes, as long as they are serializable.
No. Spring only guarantees one instance per session. Parallel requests from the same user can touch it at the same time, so protect mutable fields with synchronization or use thread-safe collections.
The default scoped proxy stands in for the real bean. Each call is routed to the instance that belongs to the current session, so the singleton never holds a stale user object.
@SessionScope makes a whole Spring bean live in the session. @SessionAttributes is a Spring MVC annotation that keeps selected model attributes of one controller in the session.
When the session expires, is invalidated (for example on logout), or the application shuts down. Destruction callbacks such as @PreDestroy run at that point.
@SessionScope gives each HTTP session its own bean instance, injected through a scoped proxy so singletons can use it safely. Use it for modest per-user state, keep it serializable and thread-safe, and choose @RequestScope or a shared store when a session bean is not the right fit.