Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What does singleton scope mean in Spring, and how is it different from the singleton design pattern?

Singleton scope means the Spring container creates one shared instance per bean definition and hands that same instance to everyone who asks for that bean. The singleton design pattern instead guarantees one instance per class in a class loader, usually with a private constructor. So in Spring, two bean definitions of the same class give two instances, new still works, and singleton beans are the default scope and are created eagerly at startup unless marked lazy.

Why this question is asked

Almost every bean we write in Spring is a singleton, so it is worth knowing exactly what that word promises. Interviewers ask this question because many developers assume that Spring singletons are the same as the classic singleton pattern, and the differences matter in tests, in multi-context applications and when we define the same class twice with different settings. In this answer, we will recap the classic pattern, see what Spring's singleton scope really means, show the differences in code, and look at when singletons are created.

1A Quick Recap of the Singleton Pattern

The singleton design pattern, described in the Gang of Four book, makes a class responsible for having only one instance. The class hides its constructor and offers a static way to get the single instance. In Java, the instance lives in a static field, and a static field exists once per class per class loader.

Java
public final class Clock {
private static final Clock INSTANCE = new Clock();
private Clock() {} // nobody else can call new Clock()
public static Clock getInstance() { return INSTANCE; }
}
Clock a = Clock.getInstance();
Clock b = Clock.getInstance();
System.out.println(a == b); // true
// new Clock(); // does not compile: constructor is private

Notice that the class itself enforces uniqueness. That makes it hard to test, because we cannot replace the instance, and it hides the dependency, because callers reach for a static method.

2What Singleton Scope Means in Spring

A scope in Spring decides how many instances of a bean the container creates and how long they live. Singleton, the default scope, means the container creates the bean once and caches it. Every injection point and every getBean() call for that bean definition receives the same cached object.

The important words are per container and per bean definition. A bean definition is the container's recipe for one bean: a class, a name, and settings. The uniqueness is a property of the container's cache for that recipe, not of the class. The class itself is an ordinary class with a public constructor.

3Seeing the Difference in Code

Here are two bean definitions that use the same class with different settings, which is common for clients that talk to different servers.

Java
public class RestGateway {
private final String baseUrl;
public RestGateway(String baseUrl) { this.baseUrl = baseUrl; }
}
@Configuration
class Gateways {
@Bean RestGateway paymentsGateway() { return new RestGateway("https://pay.internal"); }
@Bean RestGateway ordersGateway() { return new RestGateway("https://orders.internal"); }
}
var ctx = new AnnotationConfigApplicationContext(Gateways.class);
RestGateway p1 = ctx.getBean("paymentsGateway", RestGateway.class);
RestGateway p2 = ctx.getBean("paymentsGateway", RestGateway.class);
RestGateway o = ctx.getBean("ordersGateway", RestGateway.class);
System.out.println(p1 == p2); // true: same definition, same cached instance
System.out.println(p1 == o); // false: two definitions, two instances
RestGateway mine = new RestGateway("http://localhost"); // compiles and works
var ctx2 = new AnnotationConfigApplicationContext(Gateways.class);
System.out.println(p1 == ctx2.getBean("paymentsGateway")); // false: another container
Singleton pattern vs Spring singleton scope
AspectSingleton patternSpring singleton scope
Unique perclass per class loaderbean definition per container
Who enforces itthe class (private constructor)the container's cache
Can call new?noyes
Same class, two configsimpossibletwo bean definitions
How callers get itstatic getInstance()injection
Testinghard to replacepass a fake to the constructor

So there are three ways to have more than one instance of a "singleton" class: define it twice, create it with new, or start a second container. None of these is possible with the classic pattern, and all of them are useful. The second container case is common in tests, where the test framework caches several contexts in the same JVM.

4When Singletons Are Created

By default, an ApplicationContext creates all singleton beans eagerly during startup, when the context refreshes. This is a deliberate choice: configuration errors, such as a missing dependency or a bad property, show up immediately when the application starts instead of on the first request at three in the morning.

We can delay creation with @Lazy on a bean, in which case the container creates it on first use, or globally with spring.main.lazy-initialization=true in Spring Boot. Lazy creation can speed up startup in development, but it moves failures to runtime and makes the first request slower, so eager is the safer default in production. Notice also that a lazy singleton is still one instance; laziness changes when it is created, not how many there are.

Java
@Component
@Lazy
class ReportEngine {
ReportEngine() { System.out.println("ReportEngine created"); }
}
// At startup: nothing printed
// First injection or getBean(ReportEngine.class): "ReportEngine created"
// Second getBean: nothing printed, same instance returned

5Follow-up questions

Are Spring singleton beans thread-safe?

Not automatically. Spring only guarantees that one instance is shared; many request threads then call it at the same time. That is why singleton beans should be stateless, or keep their mutable state in thread-safe structures. Safe publication of the instance itself is handled by the container, but mutable fields inside it are our responsibility.

What scope should we use if we want a new instance each time?

Prototype scope creates a new instance for every request for the bean, such as each getBean() call or each injection point. Web applications also have request and session scopes. If the object needs no Spring services at all, creating it with new, or through a small factory, is often clearer than any scope.

Does @Configuration change how @Bean singletons behave?

Yes. In a @Configuration class with the default proxyBeanMethods = true, Spring subclasses the configuration with CGLIB, so calling one @Bean method from another returns the container's cached singleton instead of a new object. With proxyBeanMethods = false, or in a plain @Component, such a direct call is just a Java method call and creates a new instance.

6Conclusion

In this answer, we saw that Spring's singleton scope means one shared instance per bean definition per container, enforced by the container's cache rather than by the class. Two definitions of the same class give two instances, a second container gives another, and new still works. Singleton is the default scope, and singletons are created eagerly when the context starts unless we mark them lazy.

What to say in the interviewAbout 70 seconds aloud

Open with

In Spring, singleton scope means the container creates one instance for a bean definition, caches it, and gives that same object to everybody who asks for that bean.

Then, if they want more

It is the default scope. The classic singleton pattern is different: the class itself makes sure there is only one instance in its class loader, usually with a private constructor and a static getInstance method. A Spring singleton class is an ordinary class with a public constructor, so I can still call new on it.

And the uniqueness is per bean definition and per container: if I declare two @Bean methods returning the same class, say a payments gateway and an orders gateway with different URLs, I get two instances. If there are two application contexts, like several cached contexts in tests, each one has its own instance.

Spring creates singletons eagerly at startup by default, which surfaces configuration errors early, and @Lazy or global lazy initialization defers creation until first use. One thing worth adding is that a Spring singleton is shared across all request threads, so it should be stateless or thread-safe.

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 documentation below. 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 "What does singleton scope mean in Spring, and how is it different from the singleton...": https://scrivio-interview-library.netlify.app/spring-boot/bean-scopes/7bdb2b488c/

More in Bean scopes