@Scope tells the Spring container 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 ApplicationContext. Setting @Scope("prototype") creates a new instance on each lookup, and the web scopes request, session, application and websocket tie a bean to an HTTP request, session, servlet context or WebSocket session.
You can put @Scope on a class annotated with @Component (or a stereotype such as @Service) or on an @Bean method in a @Configuration class.
| Scope | Instances | Available in |
|---|---|---|
singleton (default) | One per container | Every application |
prototype | New instance per injection or getBean call | Every application |
request | One per HTTP request | Web applications |
session | One per HTTP session | Web applications |
application | One per ServletContext | Web applications |
websocket | One per WebSocket session | WebSocket applications |
Use the constants in ConfigurableBeanFactory and WebApplicationContext (for example ConfigurableBeanFactory.SCOPE_PROTOTYPE) instead of raw strings when you want compile-time safety.
ReportBuilder holds mutable state, so sharing one instance between threads would be a bug. Marking it as prototype gives every caller a private copy. Singleton beans, by contrast, must be stateless or thread-safe.
For a singleton the same code prints true. Two lifecycle facts matter for prototypes:
@PreDestroy on prototype beans, so you must release resources yourself.@PostConstruct, afterPropertiesSet) do run on each new instance.The most common scope bug is injecting a prototype or request-scoped bean into a singleton. Dependency injection happens once, when the singleton is created, so the singleton keeps the same instance forever. There are two clean fixes.
getObject() asks the container for a fresh prototype on every call.
@RequestScope, @SessionScope and @ApplicationScope are composed annotations. They are equivalent to @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS). The injected object is a proxy that looks up the real request-bound instance on each call, so a singleton controller can safely hold it. Without a proxy, Spring fails at startup because no request is active when the singleton is created.
You can add your own scope by implementing org.springframework.beans.factory.config.Scope and registering it with a CustomScopeConfigurer:
SimpleThreadScope ships with Spring but is not registered by default. It does not clean up automatically, so avoid it with thread pools unless you manage the lifecycle. Libraries add their own scopes too, for example @RefreshScope in Spring Cloud for beans that reload when configuration changes.
singleton for stateless services, repositories and controllers.prototype when each consumer needs its own mutable object, and inject it through ObjectProvider or a factory.request for data tied to a single HTTP call, such as a correlation id or the current tenant.session sparingly, because it stores state in memory per user and complicates horizontal scaling.singleton. One instance is created per ApplicationContext (not per JVM) and reused for every injection.
Not automatically. Spring only guarantees one instance. If the bean has mutable fields that several threads write, you must synchronise access or redesign it to be stateless.
It was injected into a singleton, so the field was set once. Inject an ObjectProvider, call ApplicationContext.getBean, or use a @Lookup method to get a new instance each time.
No. The container does not track them after creation, so @PreDestroy and DisposableBean callbacks are not invoked.
@Scope controls bean lifetime: singleton by default, prototype for per-use instances, and request, session, application or websocket scopes in web applications. Use scoped proxies or ObjectProvider when a longer-lived bean needs a shorter-lived one, and write a custom Scope only when the built-in options do not fit.