Spring bean singleton scope — Singleton pattern in Spring
Fast recall
Quick revision
Core answer
ApplicationContext and returns that managed instance to its consumers. It is not a JVM-wide GoF singleton and it does not use a public static getInstance(). Creation is container-managed, but thread safety of the bean's mutable behavior remains the application's responsibility.Interview-ready guide
Interview answer
Spring's default singleton scope means one managed instance for each bean definition in one ApplicationContext. Dependencies that refer to that definition normally receive the same object, sometimes through a framework proxy. This differs from a classic GoF singleton because the class needs no static field, private constructor, or getInstance() method; the container owns the lifecycle and dependencies remain explicit. The scope is not JVM-wide—another application context can create another instance. Spring creates and publishes the bean safely, but it does not make arbitrary mutable fields thread-safe. A service used by concurrent requests should not store request-specific data in shared fields, and any intentional shared mutable state needs synchronization or another suitable design.
Spring bean singleton scope — Singleton pattern in Spring
Direct Answer
Spring's default singleton scope keeps one bean instance for each bean definition in one ApplicationContext and returns that managed instance to its consumers. It is not a JVM-wide GoF singleton and it does not use a public static getInstance(). Creation is container-managed, but thread safety of the bean's mutable behavior remains the application's responsibility.
Common Mistakes & Pitfalls
- Mutable fields in a singleton bean.
currentUser/cartItemsare shared across all concurrent requests — classic race condition. - Thinking singleton means per-JVM. It is per
ApplicationContext; another context can create another instance, while Spring tests may reuse a cached context. - Hand-writing Singleton in Spring. Don't add
getInstance()— let the container manage the bean. - Prototype-in-singleton surprise. Injecting a prototype into a singleton gives you one prototype frozen at singleton creation, not a fresh one per call (use
ObjectProvider/lookup if you need fresh).
Check your understanding
Answer these without looking back at the article.
- What happens if you store state in a Spring singleton bean?
- What are the other bean scopes in Spring?
- What happens when you inject a prototype bean into a singleton bean?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.