Spring Bean Scopes

In this tutorial we are going to learn about various kinds of bean scopes which are available in Spring framework.

Spring Bean Definition

Beans are objects which creates the backend of your any java enterprise application. Beans are maintained by Spring IoC container. Any bean is a simple object of bean class which is instantiated, configured and also maintained by Spring IoC container. These beans have some unique configuration metadata which developers supply to Spring IOC container.

Developers provide Bean definition which contains configuration information which is known by configuration metadata, that is necessity for application’s Spring IOC container to know these following:

  • How to make a bean
  • Bean’s lifecycle and scope
  • Bean’s dependencies

Spring Bean Scopes

Bean Scope defines how many the bean instances will be created and returned by spring IOC container. In the spring bean configurations, developers define bean attribute called ‘scope’ .

In Spring, there are 5 different kinds of scopes.

  • Singleton Scope
  • Prototype Scope
  • Request Scope
  • Session Scope
  • GlobalSession Scope

There are 3 scopes mentioned by request scope, session scope and globalSession scope are only used in a web-aware kind of applications.

Spring Bean Scopes

Image Source

How to Define the Bean Scope?

This can be done in two ways.

1. In bean configuration file

2. Using annotations

Scope 1: Singleton Scope

When we define our bean by mentioning singleton scope then it means that the Spring IOC container will creates only a single object of our bean, and all client coming requests for that bean class will send that same object of that bean towards user interface.

Any rectifications to that specific object will also be mirrored in all available instances to that particular bean. Singleton scope is the default value for scope if no other scope is defined in bean configuration. Singleton bean scope is default and it enforces the Spring IOC container to have only one object for every spring container, that is independent of how much time you request for its object. This singleton behavior is maintained by bean factory itself.

Scope 2: Prototype Scope

When the prototype scope is given then the Spring IoC container makes a new bean object of the bean each time request for that particular bean is made. As a rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.

We can define prototype scope as described in the below code example.

Web Aware Scopes

For the web-aware kind of application, there are 3 more scopes available. These are not used in general prectice.

Request scope makes a bean object for every HTTP request comes from client. Session scope makes an bean object for that HTTP Session, the globalSession scope makes an object of the bean for that global HTTP Session.

Scope 3: Request Scope

For defining the bean by request scope we have to use same @Scope annotation with value of SCOPE_REQUEST:

With these kind of bean scope, new bean object will then made for every web request invoked by the client. When request gets completed, bean will not be having that scope.

Scope 4: Session Scope

For defining the bean with session scope, we have to use same @Scope annotation with value of SCOPE_SESSION:

Just as request scope, session scope ensures only one object of that bean for every session of the user. When user’s session is ended, bean will not be having that scope.

Scope 5: Global Session Scope

we define the globalSession scope with providing value of SCOPE_GLOBAL_SESSION, in annotation @Scope.

These kind of scope is generally applied in the applications with specific portlet container, and each and every portlet is associated by its unique session. Beans by Global Session scope will always be present over all the sessions instantiated by user.

global-session is always associated with Portlet kind of applications. When we define our  application to handle requests in the Portlet container then it is made upon some number of portlets. Every portlet contains its unique session, and if we wanted for our application to keep variables globally for all the portlets in our web application than we should keep them in the global-session. Global-session scope does not have any kind of special effect different then session scope defined in spring based applications.

Note: You can also define above scopes in bean configuration file.

About Author:

Shubham Sharma, currently working as Analytics Engineer in Data Science Domain. Has around 2+ years of experience in Data Science. Skilled in Python, Pandas, Anaconda, Tensorflow, Keras, Scikit learn, Numpy, Scipy, Microsoft Excel, SQL, Cassandra and Statistical Data Analysis, Hadoop, Hive, Pig, Spark, Pyspark. Connect with him at [email protected].

Comment below if you found any information missing or have queries related to above spring bean scopes tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *