Spring Bean Scopes

Spring framework supports five type of scopes and for bean instantiation as of Spring 3.0 and also we can create a custom scope.

  1. singleton
  2. prototype
  3. request
  4. session
  5. 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.


<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 />

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.

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.

This Spring tutorial was added on 19/11/2012.

Comments on "Spring Bean Scopes" Tutorial:

  1. Senthil Muthiah says:

    Good Joe. Easy to understand. Can you please write more examples on spring security.

  2. Amar Sannaik says:

    Understood. thanks.

  3. Anonymous says:

    Hi Joe,

    I look forward for your coming tutorial on Custom Scope, you discussed here in. Although, another simple and great article!

  4. Sandeep says:

    Nice explanation.. Thkx

  5. Balamurugan says:

    Great Job Joe,

    Hope this would help, taken from some site.

    5 types of bean scopes supported :

    1. singleton – Return a single bean instance per Spring IoC container
    2. prototype – Return a new bean instance each time when requested
    3. request – Return a single bean instance per HTTP request
    4. session – Return a single bean instance per HTTP session
    5. globalSession – Return a single bean instance per HTTP session & applicable for portlets
  6. Ali Jay says:

    Good Job JOE

    Superbly explained.

    Regards

  7. Joe says:

    Sure, I will write soon on Spring security.

  8. SUDHARSAN says:

    Another one nice posting..

  9. SUDHARSAN says:

    Joe, one request from my side, It would be very helpful, if you publish all your postings as in PDF format.

  10. navneetgoyal87@yahoomail.co.in says:

    Can u help me in creating view or page scope as in JSF 2.0 view scope??

  11. […] 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 […]

  12. manu says:

    nice post really doing good job,keep rocking

  13. Nagaraj says:

    Thank you

  14. Srinivas says:

    You are awesome…..

  15. Pratik says:

    Easy to understand.
    Thanks

  16. Anonymous says:

    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…

  17. Anonymous says:

    nice

  18. KOTI says:

    Hi Joe…
    This is Koti.

    Your way of posting and explaining the concepts is very nice..

    I WANT SPRING AOP PRINCIPLES
    please upload

  19. Ajit Paswan says:

    Small and effective :)

  20. Shivshankar Kadwade says:

    Hi Joe,

    nice article but its difficult to search particular topic and please upload AOP Spring.

  21. Manikavasakam says:

    Its really nice and please share all the spring tutorial as pdf format.

  22. Monish says:

    nice…

  23. Punit says:

    Reffer some article regarding the topic ApplicationContextAware

  24. vinay says:

    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.

  25. Ashutosh Malankar says:

    When we use singleton bean.
    what happen if 2 user try to access same page ??
    Will they get bean instance or diiferent.

  26. Pradeep Chinchole says:

    Nicely Explained with examples

  27. Anonymous says:

    may i know reason why spring default singleton scope?

  28. Anonymous says:

    Yes it will return the same bean instances.

  29. Priya M says:

    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.

  30. Priya M says:

    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.

  31. Akhildas says:

    how to intagrate spring and hibernate or structs to hibernate.how to devolope crude (inser,delete)

Comments are closed for this "Spring Bean Scopes" tutorial.