ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Bean Scopes in Spring Framework

Updated on September 17, 2014

Introduction

In Spring, when you configure a bean in spring configuration file, you can tell Spring about the scope for the bean. In the spring configuration file or through annotations, you can provide information to spring container about the different bean scopes. Spring container creates beans with various scopes. Spring supports various bean scopes for application context and web-aware Spring Application Context. The bean scopes which Spring supports are valid to few scenarios based on the type of Application Context.

Most frequently asked interview question over Spring is as follows :

What Bean Scopes does Spring Framework supports ?. Explain them ?.

Spring Interview Question

Bean Scopes in Spring Framework

There are 5 bean scopes available for Spring beans.

The Spring Framework supports following five scopes, three of which are available only if you use a web-aware Application Context and two when you use simple application context.

The names of 5 spring supported bean scope are as follows :

  1. Singleton Bean Scope
  2. Prototype Bean Scope
  3. Request Bean Scope
  4. Session Bean Scope
  5. Global - Session Bean Scope

Here, Singleton Bean Scope and Prototype Bean Scope is available for simple application context. Request Bean Scope, Session Bean Scope and Global - Session Bean Scope is available for web-aware application context.The figure below depicts 5 different spring supported bean scopes.

Spring Bean Scopes Video Tutorial

Lets go through each of these bean scopes one by one.

1 - Singleton Bean Scope.

Singleton scope is the default scope for Spring Bean. This bean scope has a similarity to Singleton Design Pattern in Java. In this scope, the bean definition is configured to have a single instance per Spring Inversion of Control container. In this scope, Spring instantiate bean only once. Whenever the instance of bean will be requested, spring container will return the same instance.

2 - Prototype Bean Scope.

Whenever a bean is declared as Prototype, it Scopes a single bean definition to "n" number of object instances. It is just opposite to Singleton Bean Scope. Each time a bean is requested in prototype scope a new instance gets created.

3 - Request Bean Scope.

This type of bean scope is valid only in the context of a web-aware Spring Application Context.
Spring bean which is defined in the scope of request, gets instantiated for every HTTP request made. This spring bean lives till the HTTP request lives. Each and every consecutive HTTP request has newly instantiated bean associated with it.

4 - Session Bean Scope.

This type of bean scope is valid only in the context of a web-aware Spring Application Context.
Spring bean which is defined in the scope of session, gets instantiated for handling life cycle of a HTTP Session. The spring bean configured as a Session scope survives till the HTTP Session survives. This spring bean lives till the HTTP Session lives. Each and every consecutive HTTP Session has newly instantiated bean associated with it.

5 - Global Session Bean Scope.

This type of bean scope is valid only in the context of a web-aware Spring Application Context.
Spring bean which is defined in the scope of global session, gets instantiated for handling life cycle of a Global HTTP Session.
Here, it Scopes a single bean definition to the lifecycle of a global HTTP Session. It is only valid when used in a portlet context. This global session scoped bean usually comes into picture when developing portlet based web applications.

This concept of different Scope of Spring beans is been asked frequently in Spring Interviews.

Annotation based Spring scope configuration

In order to define a bean to a particular scope use below annotations over Java bean class.

@Component annotation is used to mark a particular bean as Spring bean.

@Scope annotation is used to define scope of the bean.


Annotation based Spring scope configuration

// singleton scope
@Component
@Scope("singleton")
public class Employee { 

}

// Prototype
@Component
@Scope("prototype")
public class Employee { 

}

// session scope
@Component
@Scope("session")
public class Employee { 

}

Which bean scope have you used most ?.

See results

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article