What are Spring bean scopes and how do singleton and prototype differ?
Learn Spring bean scopes and how singleton and prototype differ in lifetime, instantiation, and lifecycle — with clear examples and interview tips.
Expected Interview Answer
A bean scope defines how many instances of a bean the Spring container creates and how long each lives. Singleton (the default) shares one instance per container, while prototype creates a brand-new instance every time the bean is requested.
Spring supports singleton and prototype in any application, plus request, session, application, and websocket scopes in web-aware contexts. A singleton is instantiated once at container startup (eagerly) and reused for every injection, so it must be stateless and thread-safe. A prototype bean is created fresh on each getBean() call or injection point, and importantly the container does not manage its full lifecycle after creation — it will not call the destroy callback.
- Singleton saves memory and construction cost by reusing one shared instance
- Prototype guarantees a fresh, isolated instance for stateful objects
- Scopes let you match object lifetime to actual usage
- Web scopes (request/session) tie beans to HTTP boundaries automatically
- Choosing the right scope prevents accidental shared-state bugs
AI Mentor Explanation
A singleton bean is like the one official match ball the umpire hands out — every bowler in the innings uses that same shared ball, so its wear affects everyone. A prototype bean is like a fresh pair of batting gloves issued to each batter who walks in; every player gets their own, and one batter sweating in them never affects the next.
Step-by-Step Explanation
Step 1
Know the default
Beans are singleton scoped unless you declare otherwise, giving one shared instance per ApplicationContext.
Step 2
Declare the scope
Annotate a bean with @Scope("prototype") or use @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) to change behaviour.
Step 3
Understand instantiation timing
Singletons are created eagerly at startup; prototypes are created lazily on each request or injection.
Step 4
Mind the lifecycle gap
Spring fully manages singleton destruction but does NOT call destroy callbacks on prototype beans.
Step 5
Handle scope mixing
Injecting a prototype into a singleton needs ObjectProvider, @Lookup, or a proxy so a fresh instance is fetched each time.
What Interviewer Expects
- Naming singleton as the default scope
- Explaining per-instance vs per-container lifetime
- Awareness that prototype destroy callbacks are not called
- Knowing web scopes: request, session, application, websocket
- The singleton-injecting-prototype problem and its fixes
Common Mistakes
- Thinking singleton means one instance per JVM instead of per container
- Assuming a prototype injected into a singleton is recreated on every use
- Putting mutable state in a singleton and causing thread-safety bugs
- Expecting Spring to destroy prototype beans
- Confusing Spring singleton scope with the GoF singleton pattern
Best Answer (HR Friendly)
“A bean scope just decides how many copies of an object Spring makes and how long they stick around. Singleton means everyone shares one copy, which is the default, while prototype means a brand-new copy is created every time something asks for it.”
Code Example
@Configuration
public class AppConfig {
@Bean // singleton by default: one shared instance
public MetricsRegistry metricsRegistry() {
return new MetricsRegistry();
}
@Bean
@Scope("prototype") // new instance on every request
public ReportBuilder reportBuilder() {
return new ReportBuilder();
}
}@Service
public class ReportService {
private final ObjectProvider<ReportBuilder> builderProvider;
public ReportService(ObjectProvider<ReportBuilder> builderProvider) {
this.builderProvider = builderProvider;
}
public Report build() {
ReportBuilder builder = builderProvider.getObject(); // fresh each call
return builder.compile();
}
}Follow-up Questions
- Why does Spring not call the destroy callback on prototype beans?
- How do you inject a prototype bean into a singleton correctly?
- What are the request and session scopes used for?
- Is a Spring singleton the same as the singleton design pattern?
- How do you make a singleton bean thread-safe?
MCQ Practice
1. What is the default scope of a Spring bean?
Unless a scope is explicitly declared, Spring creates a single shared instance per container — the singleton scope.
2. How many instances does a prototype bean produce?
A prototype bean is instantiated fresh every time it is requested from the container or injected.
3. Which lifecycle callback does Spring NOT call for prototype beans?
Spring configures and initializes prototypes but hands them off to the caller, so it never invokes their destroy method.
Flash Cards
Default Spring bean scope? — Singleton — one shared instance per ApplicationContext.
When is a prototype bean created? — Fresh on every request or injection point, lazily.
Which scope skips the destroy callback? — Prototype — Spring does not manage its full destruction lifecycle.
Web-aware scopes? — request, session, application, and websocket.
Inject prototype into singleton? — Use ObjectProvider, @Lookup, or a scoped proxy for a fresh instance.
Continue Learning
Related Interview Questions
What is the Spring bean lifecycle and what callbacks does it provide?
medium
What is the difference between @Autowired field, constructor, and setter injection?
medium
What is dependency injection in Spring and how does the IoC container work?
medium
What is @SpringBootApplication and what annotations does it combine?
easy