What are Bean Scope Types in Spring?
Bean Scope in Spring defines:
- How many bean objects Spring creates
- How long the bean object lives
- How the bean is shared inside the application
Bean scopes are managed by the Spring IoC Container.
Simple Definition
Bean Scope determines the lifecycle and visibility of Spring Beans inside the application.
Bean Scope = Rules that define bean object creation and lifecycle.
Why Bean Scope is Important?
Different applications require different object lifecycles.
Example:
- Database service may need only one object.
- User session data may need separate objects for each user.
- Temporary processing objects may need new objects every time.
Bean Scope helps manage these requirements efficiently.
Types of Bean Scopes in Spring
| Scope | Description |
|---|---|
| singleton | Only one bean object for entire container. |
| prototype | Creates new bean object every request. |
| request | One bean object per HTTP request. |
| session | One bean object per HTTP session. |
| application | One bean object per ServletContext. |
| websocket | One bean object per WebSocket session. |
1. Singleton Scope
Singleton is the default bean scope in Spring.
Only one object is created for the entire Spring Container.
Example
@Service
@Scope("singleton")
public class PaymentService {
}
How it Works
- Spring creates one object.
- Same object is reused everywhere.
Real-Time Example
Services like:
- PaymentService
- EmailService
- CourseService
usually use Singleton Scope.
Advantages
- Memory efficient
- Better performance
- Suitable for stateless services
2. Prototype Scope
Prototype scope creates a new object every time the bean is requested.
Example
@Component
@Scope("prototype")
public class ReportGenerator {
}
How it Works
- Every request gets a new object.
- Objects are not shared.
Real-Time Example
PDF generation or temporary processing objects.
Advantages
- Independent object instances
- No shared state issues
3. Request Scope
Request scope creates one bean object per HTTP request.
Example
@Component
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class RequestData {
}
How it Works
- New object created for every HTTP request.
- Object destroyed after request completion.
Real-Time Example
Request tracking, request-specific logging, or temporary request data.
4. Session Scope
Session scope creates one bean object per user session.
Example
@Component
@Scope(value = WebApplicationContext.SCOPE_SESSION)
public class UserSessionData {
}
How it Works
- One object per user session.
- Object lives until session expires.
Real-Time Example
- Shopping cart
- Logged-in user details
- User preferences
5. Application Scope
Application scope creates one bean object per ServletContext.
Example
@Component
@Scope(value = WebApplicationContext.SCOPE_APPLICATION)
public class GlobalApplicationData {
}
Real-Time Example
- Global application settings
- Shared cache data
6. WebSocket Scope
WebSocket scope creates one bean object per WebSocket session.
Real-Time Example
- Real-time chat applications
- Live notification systems
Default Bean Scope in Spring
The default bean scope is:
singleton
If scope is not specified, Spring automatically uses Singleton Scope.
Bean Scope Lifecycle Comparison
| Scope | Object Creation | Lifecycle |
|---|---|---|
| singleton | One object | Entire application |
| prototype | New object every request | Short-lived |
| request | One object per request | HTTP request duration |
| session | One object per session | User session duration |
| application | One object per application | Application lifecycle |
Real-Time Example in Dhanish Empower
| Component | Recommended Scope |
|---|---|
| PaymentService | Singleton |
| CourseService | Singleton |
| UserSessionData | Session |
| PDFGenerator | Prototype |
| RequestLogger | Request |
Advantages of Bean Scopes
- Efficient memory management
- Flexible object lifecycle management
- Improves scalability
- Supports multi-user applications
- Reduces unnecessary object creation
- Improves performance
Production-Level Importance
Bean scopes are extremely important in enterprise applications.
They help manage:
- User sessions
- Request data
- Microservices
- Cloud applications
- Real-time systems
- Memory optimization
Interview Answer
Bean Scope in Spring defines how many bean objects are created and how long they live inside the Spring IoC Container.
The main bean scopes are:
- Singleton
- Prototype
- Request
- Session
- Application
- WebSocket
Singleton is the default bean scope in Spring.
Short Interview Answer
Bean Scope defines bean object lifecycle and visibility inside the Spring IoC Container.
Frequently Asked Questions
Which is the default bean scope?
Singleton is the default bean scope.
Which scope creates new objects every time?
Prototype scope.
Which scope is used for user sessions?
Session scope.
Which scope is best for stateless services?
Singleton scope.
Can bean scopes improve performance?
Yes, proper scope management improves memory usage and scalability.
Conclusion
Bean Scope Types are very important in Spring Framework and Spring Boot.
They control object creation, lifecycle, memory usage, and sharing behavior inside the IoC Container.
Understanding bean scopes helps developers build scalable, optimized, and production-ready enterprise applications.