Spring bean scopes

15 December 2012
By Gonçalo Marques
In this tutorial you will learn about Spring bean scopes and how to use them.

Spring bean scopes

When you define a Spring bean you are actually defining it within a scope. The following five scopes are provided by Spring out-of-the-box:

Scope Description
singleton When a bean is defined with singleton scope the Spring container will assure that there will be only a single instance of this bean. Every time a singleton bean is requested by the application the container will always deliver the same bean instance.
prototype When a bean is defined with prototype scope the Spring container will always deliver a new instance of the bean to the caller.
request This scope only makes sense when Spring is used in a web application context. The Spring container will assure that it will deliver a new instance of this bean to the caller for every single HTTP request.
session This scope only makes sense when Spring is used in a web application context. The Spring container will assure that it will deliver a new instance of this bean to the caller and this same instance will be always delivered during the entire HTTP session.
globalSession This scope only makes sense when Spring is used in a portal web application context. The portlet specification states that a global session should be defined and shared amongst all the portlets that constitute a portal application. The Spring container will assure that the same bean instance will be delivered during the portlet global session lifetime.

When no scope is defined for a bean the Spring container will assume that the bean scope is singleton. This is the default Spring bean scope.

Example using annotations

If we want to define a Spring bean with prototype scope using annotations we do it like the following:

ExampleBean defined with prototype scope using annotations
package com.byteslounge.spring;

import org.springframework.stereotype.Service;

@Service
@Scope("prototype")
public class ExampleBean {

  public String sayHello() {
    return "Hello!";
  }

}

Note the prototype scope defined in the @Scope annotation.

Example using Spring configuration XML

If we want to define a Spring bean with prototype scope using the Spring configuration XML we must define it in the bean declaration:

ExampleBean defined with prototype scope in Spring configuration XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <bean id="exampleBean" 
    class="com.byteslounge.spring.ExampleBean"
    scope="prototype" />
    
</beans>

Note the prototype scope defined in the scope bean definition attribute.

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: https://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: