Why you should care about the Scope of the bean in your Spring boot app

So you are building an application using Spring Boot and using a bean to be able to inject its dependency in your code. that’s great.

When you define any class in spring boot with bean annotation, it will create a singleton object and whenever you are using this bean object it will give you the same object every single time, This is awesome, right?

Yes, but there is only one problem. Let's say you have a class that has some setter function to set some value in the class object. when your application is a multi-threaded application, there will be many threads running in parallel and if you are using the singleton object then threads will override the setter value every time you call the `setSomething()` and this is not a good sign for your application.

So what to do here?

Lucky you, Spring boot provides a feature called “bean scope” where you can define what is the scope of the bean in your application. let's go through what are the scopes spring provides.

  1. Singleton Scope

By default when you create a bean, it will always use the singleton scope and will create a singleton object (a singleton object means only one instance of an object) which will be the same throughout the application lifecycle.

Now let's see how singleton scope works

First, define a simple class which will be our bean

now let's use that bean

Let's try to run this code

As you can see sample1 value got overridden because sample1 and sample2 both as sharing the same instance of Sample class object.

2. Prototype Scope

when you use prototype scope, it will return a new instance every time requested. so let's say you have a class that has a couple of setters, now when you request a bean for this class, it will always give you a new instance of the class and you can freely use the setter for changing the object attributes and will work great in multi-threaded applications.

To use the Prototype scope just add the annotation @Scope("prototype")

Now let's try our code

See, we got 2 separate values each time because they are not sharing the same instance of the object.

There are 3 more scopes but they are not frequently used as much as the above two and are only applicable when your app is web-based. but lets talk about them anyway.

3. Request Scope

When your spring application is a web app or exposed APIs, then this scope comes in handy. When using the Request Scope, it will return the same instance of the object only for the current HTTP request lifecycle.

for example, you have one API “/user”, now when your controller is getting this request and calling your service methods, you might have a bean with Request Scope and until you send the response back for this API request every time you will get the same instance of the object but when a new request comes, it will send new instance, pretty cool right!

For example, you can define your Request scope like this

Now you will get a single instance of an object for a single request and when you get a new HTTP request, you will get a new instance.

4. Session Scope

When your spring boot application is maintaining user sessions, then this scope might help. When we use the Session Scope, for the whole session (on the user-level session) when requesting the bean, it will always return the same instance of the object. but when the user session closes you will get a new instance of the object for a new user session.

To use the session scope you need to have @Scope(WebApplicationContext.SCOPE_SESSION) but remember your spring application must be a web-aware application which means it should be a web application to receive HTTP requests.

5. GlobalSession Scope

As the name suggested when using the GlobalSession scope, it will return the same instance of the object on the global HTTP session-level, and it's only applicable for Portlet applications.

When designing your spring boot application, it's a must to use the bean scope properly. You will most probably use the first 2 scopes (Singleton and prototype) as they are widely useful.

If you find this article helpful do consider following me. I am trying to get to 100 followers. Cheers!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store