π± Spring Boot β Bean Scopes

1οΈβ£ What is a Bean Scope?
A Bean Scope defines the lifecycle and visibility of a Spring Beanβ
that is, how many instances of a bean are created and how long they live.
πΉ Why Bean Scopes Matter
- Memory management
- Performance
- Thread safety
- Correct behavior in web applications
πΉ Common Bean Scopes in Spring Boot
| Scope | Description |
|---|---|
| Singleton | One instance per Spring container |
| Prototype | New instance every time requested |
| Request | One instance per HTTP request |
| Session | One instance per HTTP session |
β Quick Summary
- Bean scope controls bean creation & lifecycle
- Choosing the wrong scope can cause bugs or memory leaks
β Interview / Exam Questions
Q1. What is a bean scope in Spring?
β‘ It defines how long a bean lives and how many instances are created.
2οΈβ£ Singleton Scope (Default)
πΉ Definition
- Default scope in Spring
- Only ONE object is created per IoC container
- Same instance is shared across the entire application
@Component
@Scope("singleton") // optional (default)
public class UserService { }
πΉ Internal Behavior (From PDF)
- Bean is created at application startup
- Eagerly initialized
- Same hashCode appears for every request
π Important:
Singleton β Thread-safe
Thread safety depends on implementation, not scope.
βοΈ Advantages
- Low memory usage
- Faster access
- Ideal for stateless services
β Disadvantages
- Not suitable for request-specific or user-specific data
β Quick Summary
- Default scope
- One object for whole application
- Created at startup
β Interview / Exam Questions
- Is Singleton scope thread-safe?
β No, thread safety must be handled explicitly. - When is a singleton bean created?
β‘ At application startup (eager).
3οΈβ£ Prototype Scope
πΉ Definition
- A new object is created every time the bean is requested
- Bean is lazily initialized
@Component
@Scope("prototype")
public class User { }
πΉ Internal Behavior (From PDF)
- Each API call produces a new instance
- Different hashCodes appear
- Spring manages creation only, not destruction
β οΈ Important:
@PreDestroy is NOT called for prototype beans.
βοΈ Advantages
- Suitable for stateful beans
- No shared state
β Disadvantages
- Higher memory usage
- Developer must manage cleanup
β Quick Summary
- New object every request
- Lazy initialization
- Spring does not manage full lifecycle
β Interview / Exam Questions
- Does Spring destroy prototype beans?
β No. - When should prototype scope be used?
β‘ For stateful or temporary objects.
4οΈβ£ Request Scope
πΉ Definition
- One bean instance per HTTP request
- Only applicable in web applications
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class RequestBean { }
πΉ Internal Behavior (From PDF)
- Bean created when HTTP request arrives
- Destroyed after request completion
- Lazy by nature
π Why Proxy Needed?
Because singleton beans may depend on request-scoped beans.
βοΈ Use Cases
- Request-specific data
- Logging request metadata
- Authentication context
β Quick Summary
- One object per HTTP request
- Automatically destroyed
- Requires proxy when injected into singleton
β Interview / Exam Questions
- Can request scope be used in non-web apps?
β No. - Why is proxyMode required?
β‘ To inject request-scoped bean into singleton.
5οΈβ£ Session Scope
πΉ Definition
- One bean per HTTP session
- Lives as long as the session is active
@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionBean { }
πΉ Internal Behavior (From PDF)
- Bean created when session starts
- Same instance used across multiple requests
- Destroyed when session expires
βοΈ Use Cases
- User preferences
- Shopping cart
- Login state
β Disadvantages
- High memory usage
- Not suitable for large-scale systems
β Quick Summary
- One bean per session
- User-specific data
- Proxy required
β Interview / Exam Questions
- Difference between request and session scope?
β‘ Request: per request, Session: per user session. - When is session bean destroyed?
β‘ On session timeout or invalidation.
6οΈβ£ Scope Interaction & Important Observations (From PDF)
πΉ Singleton + Request Scope
- Singleton bean is created at startup
- Request bean is created only when request arrives
- Singleton does not get recreated
πΉ Scope Resolution Rule
A bean with shorter lifecycle
cannot be directly injected into a bean with longer lifecycle
without proxy
β Quick Summary
- Scope mismatch causes runtime issues
- Proxy resolves lifecycle mismatch
β Interview / Exam Questions
- Can singleton depend on request bean?
β‘ Yes, using proxy. - Which scope is longest living?
β‘ Singleton.
7οΈβ£ Best Practices (Added)
β Use Singleton for:
- Services
- Repositories
- Stateless logic
β Use Prototype:
- When object state must not be shared
β Use Request / Session:
- Only in web apps
- With proxyMode
β Avoid session scope in microservices
π― FINAL INTERVIEW TAKEAWAYS
- Singleton is default
- Prototype is not fully managed
- Request & Session need proxy
- Scope questions are very common in interviews