Spring Bean Scopes

This article covers the concepts of Bean Scopes in Spring. We’ll understand when beans are created and how to configure different scopes for the beans.

By default, all Spring beans are singletons. When the container dispenses a bean (either through wiring or as the result of a call to the container’s getBean() method) it’ll always hand out the exact same instance of the bean. But there may be times when you need a unique instance of a bean each time it’s asked for.

Table 1.1 Spring’s bean scopes let you declare the scope under which beans are created without hard- coding the scoping rules in the bean class itself.
Scope What it does
singleton Scopes the bean definition to a single instance per Spring container (default)
prototype Allows a bean to be instantiated any number of times (once per use).
request Scopes a bean definition to an HTTP request. Only valid when used with a web-capable Spring context (such as with Spring MVC).
session Scopes a bean definition to an HTTP session. Only valid when used with a web-capable Spring context (such as with Spring MVC).
globalsession Scopes a bean definition to a global HTTP session. Only valid when used in a portlet context.

Singleton Scope

When you need one and only one instance of a bean, you should set the singleton property to true, as shown below:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
    <<!-- collaborators and configuration for this bean go here -->
</bean>

For more details Click Here

Prototype Scope

Prototype scope creates a new instance every time a call is made to fetch the bean. In order to achieve this functionality, all we have to do is set the singleton property to false in our config file or set the scope property to prototype.

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
    <<!-- collaborators and configuration for this bean go here -->
</bean>

For more details Click Here

The other scopes

The other scopes, namely request, session, and global session are for use only in web-based applications (and can be used irrespective of which particular web application framework)

The request scope

The Spring container will create a brand new instance of the bean for each and every HTTP request.

<!-- A bean definition with request scope -->
<bean id="..." class="..." scope="request">
    <!-- collaborators and configuration for this bean go here -->
</bean>

The session scope

The Spring container will create a brand new instance of the bean for the lifetime of a single HTTP Session.

<!-- A bean definition with session scope -->
<bean id="..." class="..." scope="session">
    <!-- collaborators and configuration for this bean go here -->
</bean>

The globalsession scope

The global session scope is similar to the standard HTTP Session scope (described immediately above), and really only makes sense in the context of portlet-based web applications. The portlet specification defines the notion of a global session that is shared amongst all of the various portlets that make up a single portlet web application. Beans defined at the global session scope are scoped (or bound) to the lifetime of the global portlet Session.

<<!-- A bean definition with globalSession scope -->
<bean id="..." class="..." scope="globalSession">
    <!-- collaborators and configuration for this bean go here -->
</bean>

One Response to Spring Bean Scopes