Spring Bean Scopes Interview Questions and Answers
Master Spring Bean Scopes with interview questions covering Singleton, Prototype, Request, Session, Application, WebSocket scopes, scoped proxies, lifecycle differences, and production best practices.
Introduction
When Spring creates a bean, it must determine how many instances of that bean should exist and how long those instances should live.
This behavior is controlled using Bean Scope.
Spring supports multiple scopes to accommodate different application types.
Common scopes include:
- Singleton
- Prototype
- Request
- Session
- Application
- WebSocket
Choosing the correct scope is important for:
- Memory efficiency
- Thread safety
- Performance
- Scalability
Spring Bean Scopes Overview
flowchart TD
SpringBean --> Singleton
SpringBean --> Prototype
SpringBean --> Request
SpringBean --> Session
SpringBean --> Application
SpringBean --> WebSocket
Q1. What is Bean Scope?
Answer
Bean Scope defines the lifecycle and visibility of a Spring Bean.
It answers two questions:
- How many bean instances should Spring create?
- How long should the bean live?
Different scopes are suitable for different application scenarios.
Q2. What is Singleton Scope?
Singleton is the default scope in Spring.
Only one bean instance exists per Spring IoC Container.
Example
@Service
public class CustomerService {
}
Equivalent
@Scope("singleton")
Singleton Scope
flowchart LR
SpringContainer --> CustomerService
User1 --> CustomerService
User2 --> CustomerService
User3 --> CustomerService
Advantages
- Memory efficient
- Fast
- Recommended for stateless services
Q3. What is Prototype Scope?
Prototype creates a new object every time the bean is requested.
Example
@Component
@Scope("prototype")
public class ReportGenerator{
}
Prototype Scope
flowchart LR
Request1 --> Bean1
Request2 --> Bean2
Request3 --> Bean3
Every request receives a new bean instance.
Q4. What is Request Scope?
Request Scope creates one bean per HTTP request.
Example
@Component
@RequestScope
public class UserContext{
}
Request Scope
flowchart LR
HTTPRequest1 --> Bean1
HTTPRequest2 --> Bean2
HTTPRequest3 --> Bean3
Used in Spring MVC and REST applications.
Q5. What is Session Scope?
Session Scope creates one bean per user session.
Example
@Component
@SessionScope
public class ShoppingCart{
}
Session Scope
flowchart LR
UserSession1 --> ShoppingCart1
UserSession2 --> ShoppingCart2
Typical use cases
- Shopping carts
- User preferences
- Login information
Q6. What is Application Scope?
Application Scope creates one bean for the entire web application.
Example
@Component
@ApplicationScope
public class GlobalSettings{
}
All users share the same bean instance.
Suitable for
- Global configuration
- Shared caches
- Application-wide metadata
Q7. What is WebSocket Scope?
WebSocket Scope creates one bean for each WebSocket session.
Example
@Component
@Scope(
value="websocket")
WebSocket Scope
flowchart LR
WebSocketSession1 --> Bean1
WebSocketSession2 --> Bean2
Useful for
- Chat applications
- Live notifications
- Real-time dashboards
Q8. What happens when a Prototype Bean is injected into a Singleton Bean?
The prototype bean is created only once during Singleton initialization.
Example
@Service
public class
OrderService{
@Autowired
ReportGenerator report;
}
The same prototype instance will be reused.
Solutions
ObjectProviderObjectFactory@Lookup- Scoped Proxy
Q9. Singleton vs Prototype
| Singleton | Prototype |
|---|---|
| One instance | New instance every request |
| Default scope | Explicit configuration |
| Memory efficient | Higher memory usage |
| Thread safety required | Naturally isolated |
| Recommended for services | Suitable for stateful objects |
Q10. Bean Scope Best Practices
Use Singleton for Services
Most business services should be stateless singletons.
Use Prototype for Stateful Objects
Example
- PDF generators
- Report builders
Use Request Scope
For request-specific information.
Avoid Session Scope Unless Necessary
Large session objects increase memory consumption.
Design Singleton Beans as Thread-Safe
Never store user-specific mutable state in singleton beans.
Banking Example
flowchart TD
CustomerController --> CustomerService
CustomerService --> CustomerRepository
CustomerController --> RequestScopeBean
CustomerController --> SessionScopeBean
SpringContainer --> SingletonService
SpringContainer --> PrototypeReportGenerator
The business service remains singleton while user-specific data uses request or session scope.
Common Interview Questions
- What is Bean Scope?
- What is Singleton Scope?
- What is Prototype Scope?
- Singleton vs Prototype?
- What is Request Scope?
- What is Session Scope?
- What is Application Scope?
- What is WebSocket Scope?
- Prototype inside Singleton?
- Bean Scope best practices?
Quick Revision
| Topic | Summary |
|---|---|
| Singleton | One instance per container |
| Prototype | New instance every request |
| Request | One bean per HTTP request |
| Session | One bean per user session |
| Application | One bean per web application |
| WebSocket | One bean per WebSocket session |
| Scoped Proxy | Access shorter-lived beans |
| ObjectProvider | Retrieve prototype beans dynamically |
| Thread Safety | Important for singletons |
| Bean Scope | Controls bean lifecycle |
Bean Scope Lifecycle
sequenceDiagram
Application->>Spring Container: Start
Spring Container->>Singleton Bean: Create Once
Client->>Singleton Bean: Reuse Same Instance
Client->>Prototype Bean: Create New Instance
HTTP Request->>Request Bean: Create
HTTP Session->>Session Bean: Create
Application Shutdown->>Singleton Bean: Destroy
Production Example – Banking Portal
A banking application contains multiple bean types.
CustomerServiceis a Singleton because it is stateless and shared across all users.TransactionReportGeneratoris a Prototype because every report generation requires a new object.RequestContextis Request Scoped to store request-specific metadata such as correlation IDs.UserSessionis Session Scoped to maintain authenticated user preferences.GlobalConfigurationis Application Scoped and stores application-wide settings.
flowchart LR
SpringContainer --> CustomerService
SpringContainer --> GlobalConfiguration
HTTPRequest --> RequestContext
UserSession --> UserPreferences
CustomerService --> TransactionReportGenerator
TransactionReportGenerator --> ReportInstance1
TransactionReportGenerator --> ReportInstance2
This design minimizes memory usage, improves scalability, and ensures user-specific state is isolated while shared services remain highly efficient.
Key Takeaways
- Bean Scope determines how many instances of a bean Spring creates and how long those instances live.
- Singleton is the default scope and is ideal for stateless business services.
- Prototype creates a new bean instance for every request and is suitable for stateful or short-lived objects.
- Request, Session, Application, and WebSocket scopes are primarily used in web applications.
- Injecting a Prototype bean directly into a Singleton does not create a new instance for every method call; use
ObjectProvider,@Lookup, or scoped proxies instead. - Singleton beans should always be designed to be thread-safe because they are shared across multiple requests.
- Avoid storing user-specific mutable state inside Singleton beans.
- Selecting the appropriate bean scope is essential for building scalable, memory-efficient, and maintainable Spring applications.