Spring framework supports five type of scopes and for bean instantiation as of Spring 3.0 and also we can create a custom scope.
- singleton
- prototype
- request
- session
- global_session
1. singleton scope
singleton is the default scope. Singleton Design Pattern requires no introduction as it is the easiest of all to understand. In the bean definition if the scope is not given, then by default singleton scope is assumed. In a given Spring container a singleton scoped bean
will be instantiated only once and the same will be used for its lifetime.
will be instantiated only once and the same will be used for its lifetime.
< bean id = zooEntity class = com .entities.Zoo /> <!-- if scope is not given, singleton is assigned as default scope, therefore both these configurations are same --> < bean id = zooEntity class = com .entities.Zoo scope = singleton /> |
2. prototype scope
prototype scope allows the bean to be instantiated whenever it is requested. Every time a separate instance is created, just opposite to singleton. Stateful beans which hold the conversational state should be declared as prototype
scope.
scope.
3. request scope
Spring bean configured as request scope instantiates the bean for a single HTTP request. The instantiated object lives through the HTTP request. This is available only for web-aware spring application context.
4. session scope
session scope is very similar to
HttpSession Scope
. Beans instantiated based on session scope scope lives through the HTTP session. Similar to request scope, it is applicable only for web aware spring application contexts.5. global_session scope
global_session scope is equal as session scope on portlet-based web applications. This scope is also applicable only for web aware spring application contexts. If this is global_session is used in normal web application (not in portlet), then it will behave as session scope and there will not be any error.
Annotation based Spring scope configuration
/** * Annotation-based configuration of session scope */ @Component @Scope ( "session" ) public class ShopCart { } |
Added to these built-in spring container scopes, we have an option to create custom scope. I will write a separate tutorial on spring custom scope.
0 comments:
Post a Comment