Spring Bean Scopes Overview
Fast recall
Quick revision
Core answer
Whether you know all five bean scopes and can explain when each is appropriate
Only mentioning singleton and prototype, forgetting the three web scopes (request, session, application)
Explain the prototype-in-singleton problem and how Spring uses scoped proxies to inject web scopes into singletons
Interview-ready guide
Interview answer
For example, OrderService is normally a stateless singleton. A request-scoped RequestContext can hold one request's correlation data and be injected through its scoped proxy. A prototype ReportBuilder injected directly into a singleton is resolved once at singleton construction; use ObjectProvider.getObject() when a new builder is required for each operation.
Spring Bean Scopes Overview
Direct Answer
A Spring scope controls how a bean instance is obtained and how long the container manages it. singleton is one instance per bean definition per ApplicationContext and is the default. prototype creates a new instance each time the container is asked for that bean. In a web-aware context, request, session, application, and websocket scopes follow their respective web lifetimes.
Code Example
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.context.annotation.SessionScope;
import java.util.ArrayList;
import java.util.List;
@Service // default singleton scope
public class OrderService {
private final OrderRepository repository;
public OrderService(OrderRepository repository) {
this.repository = repository;
}
}
@Service
@Scope("prototype") // a new instance every time
public class ReportGenerator {
private String reportData; // stateful, so prototype is needed
}
@RequestScope // one instance per HTTP request
public class ShoppingCart {
private List<String> items = new ArrayList<>();
}
@SessionScope // one instance per user session
public class UserPreferences {
private String theme;
private String language;
}
Common Mistakes & Pitfalls
- Forgetting web scopes exist: Freshers often think Spring only has singleton and prototype, missing that request, session, and application scopes exist in web apps.
- Using singleton for stateful beans: Storing user-specific data (like current cart items) in a singleton bean causes one user to see another user's data because the instance is shared across all threads.
- Not using a scoped proxy for web scopes: Injecting a request or session scoped bean into a singleton without proxyMode=TARGET_CLASS fails because Spring can't put a short-lived bean into a long-lived one — you get a BeanCreationException at startup.
Check your understanding
Answer these without looking back at the article.
- What is the default Spring bean scope?
- What is the prototype scope and when would you use it?
- What is the problem with injecting a prototype bean into a singleton?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.