Spring Bean Scopes With Examples

Spring Bean Scopes allows us to have more granular control of the bean instances creation. Sometimes we want to create bean instance as singleton but in some other cases we might want it to be created on every request or once in a session.

Spring Bean Scopes

There are five types of spring bean scopes:

  1. singleton – only one instance of the spring bean will be created for the spring container. This is the default spring bean scope. While using this scope, make sure bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues.
  2. prototype – A new instance will be created every time the bean is requested from the spring container.
  3. request – This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.
  4. session – A new bean will be created for each HTTP session by the container.
  5. global-session – This is used to create global session beans for Portlet applications.

Spring Bean Singleton and Prototype Scope

Spring bean singleton and prototype scopes can be used in standalone spring apps. Let’s see how we can easily configure these scopes using @Scope annotation.

Let’s say we have a java bean class.

Let’s define the spring configuration class where we will define the method to get MyBean instance from spring container.

Note that singleton is default scope, so we can remove @Scope(value="singleton") from above bean definition.

Now let’s create a main method and test the singleton scope.

When above program is executed, we will get output like below.

Notice that both MyBean instances have same hashcode and the constructor is called once once, it means that spring container is returning the same instance of MyBean always.

Now let’s change the scope to prototype.

This time we will get following output when main method is executed.

It’s clear that MyBean instance is created every time it’s requested from spring container.

Now let’s change the scope to request.

In this case, we will get following exception.

It’s because request, session and global-session scopes are not available for standalone applications.

Spring Bean Request and Session Scope

For spring bean request and session scope example, we will create Spring Boot web application. Create a spring boot starter project and choose “web” so that we can run it as a web application.


Our final project will look like below image.


ServletInitializer and SpringBootMvcApplication are auto generated spring boot classes. We don’t need to make any changes there.

Here is my pom.xml file, have a look at the dependencies for our application. Your pom.xml file might be slightly different based on the Eclipse version you are using.

Let’s create some spring components and configure them as spring beans in the spring container with scope as request and session.

Spring Bean Request Scope

Spring Bean Session Scope

Spring Component

Now let’s create a spring component and use spring to auto configure above beans.

Spring Rest Controller

Finally, let’s create a RestController class and configure some API end points for our testing purposes.

Spring Boot Session Timeout Configuration

Finally we have to configure spring boot session timeout variables, add below properties in src/main/resources/application.properties.

Now our spring beans with session scope will be invalidated in one minute.

Just run the SpringBootMvcApplication class as spring boot application. You should see below output for our endpoints being configured.

Spring Bean Request Scope Test

Open any browser and go to URL https://localhost:8080/nameRS and check the console output. You should see DataRequestScope Constructor Called getting printed on each request.

Spring Bean Session Scope Test

Go to https://localhost:8080/nameSS and you would get following output.


Now go to https://localhost:8080/nameSSUpdated so that DataSessionScope name value is updated to Session Scope Updated.


Now again go to https://localhost:8080/nameSS and you should see the updated value.


By this time, you should see DataSessionScope Constructor Called at XXX only once in the console output.

Now wait for 1 minute so that our session scoped bean is invalidated. Then again go to https://localhost:8080/nameSS and you should see the original output. Also you should check console message for creation of DataSessionScope again by the container.


That’s all for spring beans scope tutorial.

You can download the Spring Beans Scope Spring Boot project from our GitHub Repository.

By admin

Leave a Reply

%d bloggers like this: