bean scope
The rule for how many instances of a Spring bean exist: one shared singleton by default, or a new one per use, request or session.
In more detail
Because a singleton serves every request at once, storing request-specific data in its fields creates races and leaks data between callers. Keep singletons stateless, and choose another scope only when state ownership demands it. Scope is a design decision, not a performance switch.
Related terms
- Spring bean — An object that the Spring container creates, configures and manages, and can inject into other beans.
- mutable state — Data that can change after it is created, such as a field that is reassigned or a list that is added to.
- race condition — A bug whose outcome depends on the timing of two things happening at once.
- stateless — Describes a component or service that keeps no data from one request to the next, so each request carries everything needed to handle it.
Taught in
- Dependency Injection And The IoC Container — Modern Java: Virtual Threads & High-Throughput Services