Tutorials

Spring Bean Scope


Bean Singleton Scope


Spring provides us with facility to define scope of the beans.

We can define scope using XML configuration file or using annotation. 

Here we will focus on XML based scope declaration. Annotation based [@Scope("ScopeType")] will be explained in the respective section of this Spring tutorial.

 

Spring lets us define one of the following scopes for a bean:

  1. Singleton scope
  2. Prototype scope
  3. Session scope
  4. Request scope

Last two scopes (session and request) are applicable for web based application.

 

In this chapter of the spring tutorial, we will focus on just singleton scope.

 

Spring Singleton Scope

All the beans created in spring bean application context are by default singleton.

It means if you don't declare scope yourself for a bean it will be treated as singleton scope spring bean.

 

A bean declared with scope as singleton will return one instance of the bean definition per spring container.

This means that no matter how many times a given bean is injected into other beans, it will always be the same instance that will get injected.

Declaring a bean as singleton is pretty simple in XML configuration file.
<bean id="..." class="..." scope="singleton">
</bean>

Spring Singleton Example

Given below is an example of Singleton scope.

  • A bean mukeshBean is declared with scope singleton for class Student.java
  • A student sets his detail using set property.
  • Now the student can use this detail to fill up different kind of form objects as singleton bean will return same details that he/she filled up.

Similarly, other student can create their bean object and use it in similar manner.

In this example, we are showing how a student Mukesh Tripathi uses singleton scope.

Java POJO - Student.java

public class Student {
	private String name;
	private String stream;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStream() {
		return stream;
	}

	public void setStream(String stream) {
		this.stream = stream;
	}
}

XML Configuration

<?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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  


<bean id="mukeshBean" class="Student" scope="singleton">  

</bean>

</beans>

Test Class

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpringClass {

	public static void main(String[] args) {		
		
		ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
		
		Student student = (Student) context.getBean("mukeshBean");
		student.setName("Mukesh Tripathi");
		student.setStream("Electronics and Communication");
		
		Student studentForm = (Student) context.getBean("mukeshBean");
		
		System.out.println("::Get Form details::");
		System.out.println(studentForm.getName() +" "+studentForm.getStream());
	}

}

OUTPUT

::Get Form details::
Mukesh Tripathi Electronics and Communication
 
Did you notice that studentForm object is returned with same instance that was set for student object?
Now if you just change the scope to prototype in the applicationContext.xml file and run the same code again, you will get following output:
::Get Form details::
null null
 
We will study more in details about prototype scope in next chapter of this spring tutorial.

 



Please Share this page
Views : 59
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!