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 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.
<bean id=zooEntity class=com.javapapers.entities.Zoo /> <!-- if scope is not given, singleton is assigned as default scope, therefore both these configurations are same --> <bean id=zooEntity class=com.javapapers.entities.Zoo scope=singleton />
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.
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.
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.
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 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.
Comments are closed for "Spring Bean Scopes".
Good Joe. Easy to understand. Can you please write more examples on spring security.
Understood. thanks.
Hi Joe,
I look forward for your coming tutorial on Custom Scope, you discussed here in. Although, another simple and great article!
Nice explanation.. Thkx
Great Job Joe,
Hope this would help, taken from some site.
5 types of bean scopes supported :
Good Job JOE
Superbly explained.
Regards
Sure, I will write soon on Spring security.
Another one nice posting..
Joe, one request from my side, It would be very helpful, if you publish all your postings as in PDF format.
Can u help me in creating view or page scope as in JSF 2.0 view scope??
[…] on spring custom scope, we can define new scope as well as modify out of the box provided spring bean scopes. We will not be able to modify the standard singleton and prototype scopes but we can modify the […]
nice post really doing good job,keep rocking
Thank you
You are awesome…..
Easy to understand.
Thanks
hi Joe, what i feel is the navigation from one topic to another topic is difficult here, if possible put all related topics in place and create index/catalog for eay navigation…
say for example spring where i have to start and so on…
nice
Hi Joe…
This is Koti.
Your way of posting and explaining the concepts is very nice..
I WANT SPRING AOP PRINCIPLES
please upload
Small and effective :)
Hi Joe,
nice article but its difficult to search particular topic and please upload AOP Spring.
Its really nice and please share all the spring tutorial as pdf format.
nice…
Reffer some article regarding the topic ApplicationContextAware
how are stateful beans connected to prototype scope? whenever the bean is requested a prototype scoped bean is created by the container. how is the conversational state is maintained for a stateful beans using the prototype scope.
When we use singleton bean.
what happen if 2 user try to access same page ??
Will they get bean instance or diiferent.
Nicely Explained with examples
may i know reason why spring default singleton scope?
Yes it will return the same bean instances.
Hi Sir,
If I have an abtract class and I am configuring and I have child class which extends the abstract class.
What scope should I provide for the abstract class as well as the hild class.Should both of them be the same?
Similarly if there is a parent-child relationship in applicaton should the scopes of both the beans be the same.
Hi Sir,
If I have an abtract class and I am configuring it in my spring configuration file and I have child class which extends the abstract class.
What scope should I provide for the abstract class as well as the hild class.Should both of them be the same?
Similarly if there is a parent-child relationship in applicaton should the scopes of both the beans be the same.
how to intagrate spring and hibernate or structs to hibernate.how to devolope crude (inser,delete)