Spring Bean Scopes Examples

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Following are the supported scope by spring Framework.

  • singleton return a single bean instance per IoC container (default scope).
  • prototype return a new bean instance each time when requested.
  • session return a single bean instance per HTTP session.
  • request return a single bean instance per HTTP request.
  • globalSession return a single bean instance per global HTTP session.

Singleton scope example:

applicationContext.xml (xml based)


<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 name="employee" class="javatutsworld.Employee" scope="singleton"></bean>
 
</beans>

Employee.java

package javatutsworld;
 
public class Employee {
 
 public String empName;
 
 public String getEmpName() {
 return empName;
 }
 
 public void setEmpName(String empName) {
 this.empName = empName;
 }
 
}

Main.java

package javatutsworld;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
// Spring singleton bean test
public class Main {
 
 	public static void main(String[] args) {
 
 	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 
 	System.out.println("Spring singleton bean test");
 
 	Employee employee1 = (Employee) context.getBean("employee");
	employee1.setEmpName("Amit");
 	System.out.println("employee1 name : "+employee1.getEmpName());
  
 	Employee employee2 = (Employee) context.getBean("employee");
 	System.out.println("employee2 name : "+employee2.getEmpName());
 
 	}
 
}

Output:

Spring singleton bean test
employee1 name : Amit
employee2 name : Amit

Prototype scope Example:

applicationContext.xml (xml based)


<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 name="employee" class="javatutsworld.Employee" scope="prototype"></bean>
 
</beans>

Employee.java

package javatutsworld;
 
public class Employee {
 
 public String empName;
 
 public String getEmpName() {
 return empName;
 }
 
 public void setEmpName(String empName) {
 this.empName = empName;
 }
 
}

Main.java

package javatutsworld;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
// Spring prototype bean test
public class Main {
 
 	public static void main(String[] args) {
 
 	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 
 	System.out.println("Spring singleton bean test");
 
 	Employee employee1 = (Employee) context.getBean("employee");
 	employee1.setEmpName("Amit");
 	System.out.println("employee1 name : "+ employee1.getEmpName());
  
 	Employee employee2 = (Employee) context.getBean("employee");
 	System.out.println("employee2 name : "+employee2.getEmpName());
 
 }
 
}

Output:

Spring prototype bean test
employee1 name : Amit
employee2 name : null

Here you see that in the case of singleton scope, the second retrieval by `employee2` will display the same name which was set by `employee1, even though it's retrieved by a new getBean() method. In singleton scope, no matter how many times you retrieve it with getBean(), it will always return the same instance. In prototype scope, you will have a new instance for each getBean() method called. Hence for the second retrieval you see that name are null.

      Advertisements