The @Scope annotation tells Spring how many instances of a bean to create and how long each one lives. Without it, every bean is a singleton: one shared instance per container. With @Scope("prototype") you get a new instance on every lookup, and with the web scopes (request, session, application, websocket) the instance is tied to an HTTP request, session, servlet context or WebSocket session.
Choosing the right scope matters because it decides whether a bean may hold per-user state, whether it must be thread-safe, and when its resources are released.
| Scope | Instances | Lifetime | Available in |
|---|---|---|---|
singleton (default) | One per container | Until the context closes | Any application |
prototype | New one for each injection or getBean call | Created on demand, not tracked after creation | Any application |
request | One per HTTP request | End of the request | Web applications |
session | One per HTTP session | Until the session ends | Web applications |
application | One per ServletContext | Life of the web application | Web applications |
websocket | One per WebSocket session | Until the socket session ends | WebSocket applications |
The web scopes only work with a web-aware ApplicationContext, which Spring Boot sets up automatically for servlet and reactive web applications. Asking for a request scoped bean outside a request throws an IllegalStateException.
You can place @Scope on a class that is picked up by component scanning, or on a @Bean method inside a @Configuration class. Prefer the constants over string literals to avoid typos.
ReportBuilder keeps mutable state, so a shared singleton would mix content from different callers. As a prototype, every consumer gets a fresh builder.
A singleton is created once per Spring container (not once per JVM, and not the same thing as the GoF singleton pattern). Use it for stateless services, repositories, configuration and shared clients. Because many threads call the same instance, a singleton must not hold mutable per-request state.
A prototype bean is instantiated each time it is requested. Spring runs the initialisation callbacks (@PostConstruct, InitializingBean) but does not manage the rest of its life: destruction callbacks such as @PreDestroy are not called, so you must release any resources yourself.
For web scopes, Spring provides composed annotations that already set the correct scope and a scoped proxy: @RequestScope, @SessionScope and @ApplicationScope in org.springframework.web.context.annotation.
A session scoped bean should be Serializable if your container persists or replicates sessions. Request scoped beans are a clean way to carry per-request data (such as a correlation ID) without passing it through every method signature.
A singleton is wired once, when the container starts. If you inject a prototype or request scoped bean into it directly, the singleton keeps that single instance forever, which defeats the scope. There are two standard fixes.
proxyMode makes Spring inject a proxy that looks up the real bean for the current request or session on each call. The @RequestScope and @SessionScope annotations already do this.
ObjectProvider<T> asks the container for a new instance whenever you call getObject().
You can implement the org.springframework.beans.factory.config.Scope interface and register it with a CustomScopeConfigurer. Spring ships SimpleThreadScope (name thread) as an example, but it is not registered by default and it does not clean up thread-bound beans for you. Libraries such as Spring Cloud add their own scopes, for example refresh. Reach for a custom scope only when none of the built-in ones fit.
With virtual threads in Java 21 and later, a thread-bound scope can leak state or create very many instances, so prefer request scope or an explicit context object for per-task data.
The default scope is singleton: one shared instance per application context. You do not need to write @Scope("singleton").
No. Spring creates one instance but does nothing to protect its fields. Keep singletons stateless or make their state immutable or properly synchronised.
It was injected into a singleton, so it was resolved only once. Inject an ObjectProvider or use a scoped proxy to get a new instance for each use.
No. The container hands over a prototype and forgets it, so you are responsible for releasing its resources.
@Scope controls the number and lifetime of bean instances. Use the default singleton for stateless collaborators, prototype for stateful helpers, and the request, session and application scopes (preferably via @RequestScope, @SessionScope, @ApplicationScope) for web state. When a shorter-lived bean is used inside a singleton, go through a scoped proxy or ObjectProvider, and remember that prototype beans are not destroyed by the container.