Spring

Spring Bean Scopes

Introduction:

Spring core container instantiates beans and manages their life-cycle. While defining a bean, we can provide its scope. Unless explicitly provided, singleton is the default scope of beans in a Spring container.

We have five types of bean scopes available in Spring. In this tutorial, we’ll explore each of them.

1. singleton:

The singleton scope ensures that there exists only a single instance of that bean per Spring IoC container. This single bean instance is stored in the cache of all singleton beans and the reference to this cached object is returned for each subsequent requests.

As we know, all Spring beans are singleton by default.

Let’s say we have a Student bean:

The Student class will be a singleton bean by default. Let’s try to load it in our main() method:

Clearly, the existing instance of Student bean has been returned for the second invocation. In other words, both student1 and student2 references are pointing to the same object.

2. prototype:

For a prototype-scoped bean, a new instance is created for every request of that specific bean within our application code.

We can either configure our bean’s scope as a prototype using @Scope annotation:

Or when using XML-based configurations, we’ll have:

Now, let’s see what happens in our main() method:

Here, student2 refers to a new instance of Student class with its name attribute as null.

Note that we should always use prototype scope for the stateful beans and singleton for the stateless beans.

3. request:

When using the request scope, a new instance is created for each and every HTTP request. The bean instance is destroyed on the HTTP request completion.

We can configure the request scope either using annotation:

Or via XML configuration:

4. session:

For a session scope, the Spring container creates a new bean instance for each HTTP session. All the HTTP requests within an HTTP session will share the same bean instance.

To configure a session-scoped bean, we’ll use:

For a xml-based configurations we’ll have:

The container will destroy the session-scoped bean instances as and when that HTTP session closes.

5. application:

With application scope, a web-aware container creates a bean instance per ServletContext. Note that the application scope is different from the singleton scope:

  • We can use the singleton scope in a standalone application. The application scope is only valid for a web-based Spring container/application
  • For singleton scope, a single instance exists per application context. On the other hand, the application-scoped bean has a single instance for the entire ServletContext. We might have multiple applications sharing the same ServletContext

Let’s configure our Student bean as an application-scoped bean:

The xml based definition would look like:

Conclusion:

In this tutorial, we explored various Spring bean scopes.

Note that the request, session and application bean scopes are only valid for a web-aware Spring ApplicationContext.

 

One comment
Pingback: Spring Bean Scopes | Hey Android - Android World

Leave a Comment

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