Spring Bean Scopes Singleton Prototype Request Session
Understand Spring bean scopes: singleton, prototype, request, session and application, how each behaves, and the thread safety traps of the default.

A scope controls how many instances of a bean exist and how long they live. Most beans never leave the default, which is also the source of most threading mistakes.
Singleton
@Service
class PriceService {
private BigDecimal lastQuote; // shared by every request thread
BigDecimal quote(String sku) {
lastQuote = lookup(sku); // data race
return lastQuote;
}
}One instance serves all callers on all threads. The bean is fine while it holds only final dependencies. The moment it stores per-request data in a field, two requests overwrite each other. Keep singletons stateless, or guard shared state with the techniques in volatile vs synchronized. Singleton here means one per context, so two contexts in a test hold two instances.
Prototype
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
class ReportBuilder { /* holds per-report state */ }Each lookup creates a new object. Injecting a prototype into a singleton does not give a new one per call, because injection happens once. That trap has its own post: injecting a prototype into a singleton. Spring creates the prototype but never calls its destruction callbacks.
Web scopes
| Scope | Lives for | Annotation shortcut |
|---|---|---|
request | One HTTP request | @RequestScope |
session | One user's HTTP session | @SessionScope |
application | The servlet context | @ApplicationScope |
websocket | A WebSocket session | @Scope("websocket") |
@Component
@RequestScope
class RequestContext {
private String traceId;
String traceId() { return traceId; }
void setTraceId(String id) { this.traceId = id; }
}These need a web-aware context, which Spring Boot provides. Using one outside a request gives IllegalStateException: No thread-bound request found.
Injecting a narrow scope into a wide one
A singleton is created once, but a request bean does not exist yet at that time. The annotations above use a scoped proxy by default, which injects a stand-in that finds the real bean for the current request at call time:
@Service
class AuditService {
private final RequestContext context; // a proxy, resolves per request
AuditService(RequestContext context) { this.context = context; }
}A class-based proxy needs a non-final class with a proxyable constructor. The proxy mechanics are the topic of JDK and CGLIB proxies.
Choosing
Prefer a stateless singleton and pass request data as parameters. Use request scope for cross cutting data like a trace id, session scope sparingly because it ties memory to every logged in user, and prototype for builder-like objects. In reactive or virtual-thread code, thread-bound request scope does not carry over unless the framework propagates it.
Singletons must be thread-safe
A singleton bean is shared by every request, and web requests run in parallel. A field that holds per-request data in a singleton is a race condition that survives code review, because it works with one user. Keep singletons stateless, or make their state safe: immutable fields set in the constructor, a thread-safe collection, or an atomic counter. The usual accident is a service with a field like currentUser or a reusable SimpleDateFormat, and the symptom is data that appears in the wrong user's response under load and never in a developer's test.
Prototype does not mean what people hope
A prototype bean is created on each request to the container, and Spring does not manage its destruction, so @PreDestroy is never called. More importantly, when a singleton injects a prototype, the injection happens once, at creation, and the singleton keeps that one instance forever, so the prototype scope has no effect. To get a fresh instance on each use, inject an ObjectProvider<MyPrototype> and call getObject(), or use a lookup method. People who find "my prototype is always the same object" have met this.
Scoped proxies for web scopes
A request-scoped or session-scoped bean cannot be injected directly into a singleton, because the singleton is created before any request exists. Declare the narrow bean with @RequestScope or @SessionScope, which apply a scoped proxy by default, and the singleton receives a proxy that resolves the right instance for the current request at each call. Calling it outside of a request, from a scheduled job or a new thread, fails with No thread-bound request found, which means that the code is not running in a request and should not use that bean.
Session scope is memory
A session bean lives as long as the session, which may be thirty minutes after the last click, times the number of users. A few kilobytes per session is fine, and a cached result list of megabytes per user is an outage waiting for a busy day. Store identifiers, not object graphs. With several instances, session data must be shared or the load balancer must be sticky, and session beans must be serializable if the sessions are replicated.
Virtual threads and request scope
Request-scoped beans rely on thread-bound request attributes, and Spring propagates them to the thread that handles the request, virtual or not. A task that you start on another executor does not inherit them, so pass the data it needs as an argument and do not reach for the request from there. The same rule held with platform threads, and it is the cause of a lot of intermittent null values.
A rule to pick the scope
Start with singleton and write stateless code, which covers nearly everything. Use a request scope for data that belongs to one request and is shared by several beans in it, such as a correlation context. Use prototype for objects that hold state and are cheap to create, and fetch them through a provider. Avoid session scope, and keep state in a store or a token, since it makes the application easier to scale.
Testing scope behavior
Write a test that fetches a prototype bean twice from the context and asserts that the two are different objects, and that a singleton fetched twice is the same one. For a request scope, use MockMvc or a RequestContextHolder set up in the test, and assert that two requests see separate instances. A one-line test of the scope settles an argument that otherwise starts again with every new teammate.
Scopes and testing helpers
In tests, @SpringBootTest gives each test class a context that is cached across classes, so singleton state persists between test classes that share it. If a test changes the state of a singleton, reset it in @AfterEach, or mark the class @DirtiesContext as a last resort, since that discards the context and makes the suite slower for every class that follows.
Where this does not apply
Scopes decide instance count, not transactions or ownership. For the lifecycle each instance goes through, see the bean lifecycle. The reference is Bean Scopes, the [@Scope Javadoc](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Scope.html) lists the constants, and the Spring MVC guide shows where requests begin.


