Spring Bean Scopes

By Default Spring Bean scope is singleton that is only one bean is created per spring container and shared by other objects. Spring has <bean> tag in which scope is define. To force Spring to produce a new bean instance each time one is needed, you should declare the bean's scope attribute to be prototype.

Syntax : <bean id = "service" class = "com.jtechies.Service" scope="prototype">

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

Scope
Description
singleton This scopes the bean definition to a single instance per Spring container. It is by default scope set by spring.
prototype This scopes a single bean definition to a new bean instance each time when requested
request This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
session This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
global-session This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

Spring Singleton Scope Example

HelloSpring.java File
package com.jtechies;

public class HelloSpring {
	private String message;

	public void setMessage(String message){
	      this.message  = message;
	}
	public String getMessage() {
		return message;
	}
}
Test.java File
package com.jtechies;

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

public class Test {
   public static void main(String[] args) {
      ApplicationContext context = new 
      	ClassPathXmlApplicationContext("Beans.xml"); 
      HelloSpring obj1 = (HelloSpring) 
      	context.getBean("helloSpring");
      obj1.setMessage("Hello spring this 
      	is a singleton example !!!");
      System.out.println(obj1.getMessage());
      HelloSpring obj2 = (HelloSpring) 
      	context.getBean("helloSpring");
      System.out.println(obj2.getMessage());
   }
}
Beans.xml File
<?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="helloSpring" class="com.jtechies.HelloSpring"
   	scope="singleton"/>
</beans>
Output
Hello spring this is a singleton example !!!
Hello spring this is a singleton example !!!

Spring Prototype Scope Example

HelloSpring.java File
package com.jtechies;

public class HelloSpring {
	private String message;

	public void setMessage(String message){
	      this.message  = message;
	}
	public String getMessage() {
		return message;
	}
}
Test.java File
package com.jtechies;

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

public class Test {
   public static void main(String[] args) {
      ApplicationContext context = new
      	ClassPathXmlApplicationContext("Beans.xml"); 
      HelloSpring obj1 = (HelloSpring)
      	context.getBean("helloSpring");
      obj1.setMessage("Hello spring this 
      	is a prototype example !!!");
      System.out.println(obj1.getMessage());
      HelloSpring obj2 = (HelloSpring)
      	context.getBean("helloSpring");
      System.out.println(obj2.getMessage());
   }
}
Beans.xml File
<?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="helloSpring" class="com.jtechies.HelloSpring"
   	scope="prototype"/>
</beans>
Output
Hello spring this is a prototype example !!!
null