🚀 Want to Master Spring & Java Development?
Subscribe to Ram N Java for beginner-friendly coding tutorials and practical guides!
🔔 Subscribe to YouTube ChannelUnderstanding Spring Bean Scopes: Complete Overview
In the Spring Framework, defining a bean does not merely create an object recipe—it also defines the lifecycle and visibility of the instances created from that definition. A Bean Scope dictates how Spring manages, shares, and instantiates bean instances across your application. Understanding bean scopes is essential for writing thread-safe, scalable, and memory-efficient enterprise applications.
1. Core Spring Bean Scopes
Spring provides two primary core scopes available in any standalone application context:
- Singleton (Default): Scopes a single bean definition to a single object instance per Spring IoC container. Whenever a bean is requested, Spring returns the exact same shared instance. Best suited for stateless beans like service and DAO components.
- Prototype: Scopes a single bean definition to create a new object instance every time a request for that bean is made. Ideal for stateful objects where individual callers require isolated instance state.
2. Web-Aware Spring Bean Scopes
When running in a web-aware Spring ApplicationContext (such as Spring MVC or Spring Boot web applications), additional scopes become available:
- Request: Scopes a single bean definition to the lifecycle of a single HTTP request. Each HTTP request receives its own distinct bean instance.
- Session: Scopes a bean definition to the lifecycle of an HTTP Session. Ideal for user-specific data such as shopping carts or login sessions.
- Application: Scopes a bean definition to the lifecycle of a
ServletContext, shared across all servlets in the web application. - Websocket: Scopes a bean definition to the lifecycle of a WebSocket session.
3. How to Define Bean Scopes
Spring provides flexible ways to declare the scope of a bean:
- XML Configuration: Use the
scopeattribute inside the<bean>tag:<bean id="myBean" class="com.example.MyBean" scope="prototype"/>. - Annotation-Based: Annotate your component or
@Beanmethod with@Scope("prototype")or standard constants fromConfigurableBeanFactory/WebApplicationContext.
4. Best Practices & Considerations
Keep these key architectural points in mind:
- Thread Safety: Because singleton beans are shared across threads, ensure their member fields are immutable or stateless.
- Prototype Destruction: Spring does not manage the complete lifecycle of prototype beans after instantiation; clients are responsible for cleaning up prototype instances and releasing acquired resources.
📺 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=dY3ua64_g2Y&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
| What are the different types of spring bean scopes |
See also:
No comments:
Post a Comment