Spring – Bean Scopes

  1. Home
  2. Blog
  3. Spring – Bean Scopes

Spring – Bean Scopes

When we define a bean in Spring, we declare a scope for that bean. For example, if we want Spring to return same bean instance each time one is required, we should declare bean’s scope attribute to be singleton. Similar way to force Spring to produce a new bean instance each time one is required, we should declare bean’s scope attribute to be prototype.

Spring Framework supports following five scopes, three of which are available only if you use a web-aware ApplicationContext.

Scope Description
prototype Returns new bean instance for every request
singleton Return single bean instance per Spring IoC container
global-session Return single bean instance per global HTTP session
session Return single bean instance per HTTP session
request Return single bean instance per HTTP request

Singleton Scope:

When scope is set to singleton, Spring IoC container creates one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.

The default scope is singleton, however when we need one and only one instance of a bean, we can set scope property to singleton in the bean configuration file, as shown below:

<!-- A bean definition with singleton scope -->
<bean id="demoMessage" class="com.ms.DemoMessage" scope="singleton">
   <!-- More beans definition goes here -->
</bean>

Let’s setup Eclipse for this demo.

- Create a project with a name DemoExample and create a package com.ms under the src folder in the created project.
- Add required Spring libraries.
- Create Java classes DemoMessage and DemoApp under the com.ms package.
- Create Beans configuration file demoBeans.xml under the src folder.
- Create content of all the Java files and Bean Configuration file and run the application.

Create a class for this example

package com.ms;

public class DemoMessage {
   private String demoMessage;

   public void setDemoMessage(String demoMessage){
      this.demoMessage= demoMessage;
   }

   public void getDemoMessage(){
      System.out.println("Demo Message : " + demoMessage);
   }
}

Below DemoApp.java will read demoBeans.xml and set appropriate mapping to get output.

package com.ms;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ms.DemoMessage;

public class DemoApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("demoBeans.xml");

    // Create first Object and set/get few attributes
      DemoMessage demoMessage1 = (DemoMessage) context.getBean("demoMessage");
      demoMessage1.setMessage("First Demo Message");
      demoMessage1.getMessage();

    // Create Second Object and get few attributes
      DemoMessage demoMessage2 = (DemoMessage) context.getBean("demoMessage");
      demoMessage2.getMessage();
   }
}

Beans mapping file [demoBeans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="demoMessage" class="com.ms.DemoMessage" scope="singleton">
   </bean>
  
</beans>

Once we are done with setup, let’s see the application output

Demo Message : First Demo Message
Demo Message : First Demo Message

Prototype Scope:

When scope is set to prototype, Spring IoC container creates new bean instance of object every time a request for that specific bean is made.

Note: As a rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.

Let’s define property scope for a bean. Yes, we just have to changes the scope property of the bean to “prototype”

<!-- A bean definition with singleton scope -->
<bean id="demoMessage" class="com.ms.DemoMessage" scope="prototype">
   <!-- More beans definitions goes here -->
</bean>

Now let’s run application again and see the output.

Demo Message : First Demo Message
Demo Message : null

 

Let's Share
Show Buttons
Hide Buttons