Quick revision
The short answer
A Spring singleton-scoped bean is one instance per bean definition per application context, while a GoF singleton is one instance per class per class loader, reached through a static method. So two bean definitions or two contexts, such as separate test contexts or parent and child contexts, give separate instances. A classic static singleton still causes problems in Spring applications because it is a hidden dependency that tests cannot replace, and when it holds mutable state it leaks data between tests and tenants and can keep class loaders alive. We prefer container-managed beans injected through constructors, and keep static singletons for stateless, dependency-free utilities.
What to remember
In this answer, we saw that a GoF singleton is one instance per class per class loader, reached statically, while a Spring singleton is one instance per bean definition per application context, injected by the container. Several definitions, test contexts or parent and child contexts therefore give several instances. Static singletons still cause trouble in Spring applications as hidden dependencies and, when mutable, by leaking state across tests and tenants and pinning class loaders, so we keep them for stateless utilities and use injected beans for everything else.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A Spring singleton-scoped bean is one instance per bean definition per application context, while a GoF singleton is one instance per class per class loader, reached through a static method. So two bean definitions or two contexts, such as separate test contexts or parent and child contexts, give separate instances. A classic static singleton still causes problems in Spring applications because it is a hidden dependency that tests cannot replace, and when it holds mutable state it leaks data between tests and tenants and can keep class loaders alive. We prefer container-managed beans injected through constructors, and keep static singletons for stateless, dependency-free utilities.
Why this question is asked
Spring's default scope is called singleton, and many developers assume it means the same as the pattern they learned from the Gang of Four book. The difference matters in practice: it explains why a bean can have two instances, why a test sees state from another test, and why a cache in a static field can mix up customers. Interviewers ask this to see whether we know what the container really guarantees. In this answer, we will compare the two meanings, see how Spring creates one instance per bean definition per context, look at where several contexts appear, and then go through the problems a static singleton still causes and what to keep static.
1Two Meanings of Singleton
The GoF singleton, from the Design Patterns book, is a class that ensures it has only one instance and provides a global access point to it. In Java, that usually means a private constructor and a static method or field. The uniqueness is per class per class loader, because a static field belongs to a class as loaded by one class loader.
A Spring singleton is a scope, a rule the container follows when creating beans. For each bean definition, a singleton-scoped bean is created once per application context and that same instance is injected wherever it is needed. The class itself knows nothing about this; it has a normal public constructor and could be created any number of times outside the container.
| Aspect | GoF singleton | Spring singleton bean |
|---|---|---|
| Unique per | Class per class loader | Bean definition per context |
| Who enforces it | The class itself | The container |
| How callers reach it | Static method, Registry.get() | Constructor injection |
| Replaceable in tests | Hard | Easy, inject a fake |
| Can have two instances | Only by breaking the pattern | Yes, two definitions or contexts |
2One Instance per Bean Definition per Context
Because the uniqueness belongs to the bean definition, two definitions of the same class give two instances, each a singleton in its own right.
Notice that this is a feature. It is exactly what we want for two HTTP clients with different base URLs, and it is why Spring's model is more flexible than the classic pattern: the number of instances is a configuration decision, not something baked into the class.
3Where Several Contexts Appear
In a running application there is usually one main context, but several contexts are common elsewhere. The Spring TestContext framework caches application contexts by their configuration, so test classes with different configuration, different @MockitoBean declarations or different active profiles each get their own context, with their own instances of every singleton bean. Two singleton beans that we think of as the same are then two objects.
Context hierarchies create more. A parent context can hold shared beans while child contexts hold their own, as in classic Spring MVC setups with a root context and a servlet context, or in libraries that create a child context per client. A bean defined in both parent and child gives two instances. None of this is a problem for stateless beans, but code that assumes there is exactly one instance in the JVM, for example to count requests globally, will be wrong.
4Static Singletons as Hidden Dependencies
A static singleton does not appear in the constructor of the class that uses it. The caller just reaches for it.
The second version is the same logic, but the dependency is visible. A test can pass in a TaxTable with fixed rates, and Spring injects the real singleton-scoped bean in production. Reading the constructor tells us everything the class needs.
5Shared Mutable State: Tests, Tenants and Class Loaders
A static singleton that holds mutable state, such as a cache or a counter, adds three further problems. First, tests leak into each other: all tests in the same JVM share the static field, so one test's cached values change another test's result, and the order of tests starts to matter. Spring contexts can be closed and recreated; a static field cannot.
Second, tenants leak into each other. In a multi-tenant service, a static cache keyed without the tenant serves one customer's data to another, and because it lives outside the container, request-scoped tenant information is easy to forget. Third, static fields can keep class loaders alive. In application servers, test runners and development tools that reload code, a static field in a shared class that refers to an object from the application's class loader keeps that whole class loader and all its classes in memory after the application is undeployed, a common cause of metaspace leaks.
6What to Keep Static
Static singletons are not forbidden. They are fine for things that are stateless and have no dependencies of their own: a utility class of pure functions, a constant such as a pre-compiled Pattern, or a value object like Comparator.naturalOrder(). These never need to be replaced in tests and never hold data that could leak.
For anything with state, configuration or collaborators, we let the container manage it as a bean and inject it through the constructor. That keeps one instance where we want one, makes the dependency visible, and lets tests and tenants have their own.
7Follow-up questions
Is a Spring singleton bean thread-safe?
Not automatically. The container creates one instance and shares it between all threads that use it, so the bean must be stateless or protect its own mutable state. Singleton scope is about how many instances exist, not about concurrency.
Can a Spring singleton bean depend on a prototype bean?
Yes, but it receives one prototype instance at creation time and keeps it. To get a new prototype instance on each use, inject an ObjectProvider and call getObject(), or use a lookup method.
Why do my tests sometimes create many Spring contexts?
Because the TestContext framework caches contexts by their configuration, and each different set of configuration classes, profiles, properties or mock bean declarations creates a new one. Each new context means new instances of every singleton bean and a slower test suite.
8Conclusion
In this answer, we saw that a GoF singleton is one instance per class per class loader, reached statically, while a Spring singleton is one instance per bean definition per application context, injected by the container. Several definitions, test contexts or parent and child contexts therefore give several instances. Static singletons still cause trouble in Spring applications as hidden dependencies and, when mutable, by leaking state across tests and tenants and pinning class loaders, so we keep them for stateless utilities and use injected beans for everything else.
Open with
The GoF singleton is enforced by the class itself: a private constructor and a static accessor, so there is one instance per class per class loader. A Spring singleton is just a scope: the container creates one instance per bean definition per application context and injects it.
Then, if they want more
So if I declare two beans of the same class, say two RestClients with different URLs, I get two instances, and if there are two contexts, like different cached test contexts or a parent and child context, each has its own instance. That flexibility is useful, but code that assumes one instance per JVM is wrong in those cases.
A classic static singleton still causes problems in a Spring app for a few reasons. It is a hidden dependency: a class calls TaxTable.getInstance inside a method, so the constructor does not show it and tests cannot swap it without static mocking.
If it holds mutable state, like a cache, that state leaks between tests running in the same JVM and between tenants if the key forgets the tenant, and in environments that reload code it can keep an old class loader alive.
So I would make such things beans and inject them through constructors, and keep static singletons only for stateless, dependency-free utilities and constants.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How is a Spring singleton-scoped bean different from a GoF singleton, and in which...": https://scrivio-interview-library.netlify.app/java/creational-patterns/64fbfc9ebe/