Difficulty: Beginner
What are the different bean scopes in Spring? What happens if a singleton bean holds mutable state?
Bean scope answers a simple question: how many instances of this bean does the container create, and for how long does each live? Get this wrong and you end up with bugs that only appear under load, which is why interviewers love the follow-up about mutable state in singletons.
The default scope is singleton. One instance per ApplicationContext, created at startup and shared by everyone who injects it. Note that this is different from the Gang of Four singleton pattern, which is one instance per JVM class loader. Two different contexts can each have their own singleton of the same class. The second scope is prototype: a brand new instance every time the bean is requested from the container. Then there are the web scopes, available only in a web-aware context: request, one instance per HTTP request; session, one per user session; application, one per ServletContext; and websocket, one per WebSocket session.
Now the trap. Because every request thread shares the same singleton, any mutable instance field in a controller or service is shared state. Imagine a service with a private String currentUser field that you set at the start of a request. Two users hit the endpoint at almost the same time, thread B overwrites the field, and thread A ends up processing the wrong user's data. This is a genuine data leak, not just a glitch. The fix is to keep singleton beans stateless: pass state through method parameters and local variables, and keep only injected collaborators and immutable configuration in fields.
When you genuinely need per-request state, use a request-scoped bean or simply pass an object. There is a wrinkle, though: injecting a shorter-lived bean into a longer-lived one. A singleton is created once, so it would receive a single request-scoped instance at startup, which fails since no request exists. The solution is a scoped proxy. Declaring @RequestScope, or proxyMode = ScopedProxyMode.TARGET_CLASS, makes Spring inject a proxy that looks up the real request bean each time a method is called. The same applies for prototype beans injected into singletons: the singleton gets one prototype at creation and never a new one. Use ObjectProvider, Provider, or a lookup method to get a fresh instance each time.
Another point interviewers appreciate is that Spring does not manage the full lifecycle of prototype beans. It creates and wires them and then forgets them, so @PreDestroy callbacks are not called for prototypes. If they hold resources you must release them yourself.
In practice, over ninety percent of your beans should be singletons and stateless. Scope is a tool for the exceptions, and reaching for it often usually signals a design that should pass data explicitly instead.
@Component
@Scope("prototype")
class ReportBuilder { } // new instance per lookup
@Component
@RequestScope // proxied automatically
class RequestContext {
private String traceId = UUID.randomUUID().toString();
public String getTraceId() { return traceId; }
}
@Service
class BadService {
private String currentUser; // shared across all threads: BUG
public void handle(String user) { this.currentUser = user; }
}
@Service
class ReportService {
private final ObjectProvider<ReportBuilder> builders;
ReportService(ObjectProvider<ReportBuilder> builders) { this.builders = builders; }
public ReportBuilder newReport() { return builders.getObject(); } // fresh each call
}
RequestContext can be safely injected into singletons because the proxy resolves the real bean per request. ObjectProvider gives a fresh prototype each call.
Bean Scope, Singleton, Prototype, Request Scope