🚀 Want to Master Spring & Java Development?
Subscribe to Ram N Java for beginner-friendly coding tutorials and practical guides!
🔔 Subscribe to YouTube ChannelWhat Are Spring Bean Scopes in the Spring Framework?
When you define a bean in the Spring Framework, you are creating a recipe for creating instances of that class. The bean scope defines how Spring manages the lifecycle, creation, and sharing of those instances across your application. Grasping how bean scopes function is fundamental to designing robust, scalable, and memory-efficient enterprise Spring applications.
1. The Concept of Bean Scopes in Spring
A bean scope tells the Spring IoC container whether to reuse an existing instance or create a fresh one every time a component requests that bean:
- Lifecycle Management: Dictates when a bean is created, how long it remains in memory, and when it is ready for garbage collection.
- State Sharing vs. Isolation: Controls whether state changes made to a bean are visible across the entire application or isolated to a specific thread or user interaction.
- Resource Optimization: Prevents unnecessary instantiation of heavyweight objects like database connection pools or service managers.
2. Core Scopes: Singleton vs. Prototype
The two foundational scopes supported in all standard Spring IoC containers are:
- Singleton (Default): The IoC container creates exactly one shared instance of the bean per container context. Every request for the bean returns that identical cached instance. It is perfect for stateless services, repositories, and controllers.
- Prototype: A new instance is created every time the bean is requested from the container (via injection or
getBean()). It is intended for stateful objects where each consumer needs an isolated copy.
3. Web-Aware Scopes
When working inside web-aware application contexts (such as Spring MVC or Spring Boot web applications), Spring introduces specialized scopes:
- Request: Bound to the lifecycle of a single HTTP request. Each HTTP request gets its own bean instance.
- Session: Bound to an HTTP session lifecycle, ensuring user-specific data stays isolated between sessions.
- Application: Scoped to the lifecycle of a
ServletContext, shared across the whole web context. - Websocket: Scoped to a particular WebSocket session lifecycle.
4. Key Best Practices
To avoid common pitfalls when working with Spring bean scopes:
- Keep singleton beans stateless to ensure thread safety across concurrent requests.
- Be mindful of injecting a prototype bean into a singleton bean—since singletons are instantiated once, the prototype dependency will only be resolved once unless scoped proxies or lookup methods are used.
📺 Explore More Spring Framework Tutorials
Continue expanding your Java and Spring knowledge with these related tutorials from our channel:
Click here to watch on Youtube:
https://www.youtube.com/watch?v=LaccwPDLF3Y&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
| What are spring bean scopes |
See also: