Spring Bean Scopes With Example

In This Article we will learn spring bean scopes with Examples
spring supports following scopes

  • singleton-Scopes a single bean definition to a single object instance per Spring IoC container.
  • prototype-Scopes a single bean definition to any number of object instances.
  • request-Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
  • session--Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
  • globalsesion-Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a Portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

singleton scope

The default spring bean scope is singleton. when you define a bean definition and it is scoped as a singleton, the Spring IoC container creates exactly 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.
if spring bean scope is singleton then spring container makes spring bean as a singleton wih respect to its id.
Lets See An one example for spring bean scope singleton

Tools Used In This Article

  • Spring 4.3.2
  • Maven 4.0.0
  • JDK 1.7

Project Directory Structure

Maven Dependencies

Add The Following Dependencies to the pom.xml
  • spring-core
  • spring-beans
  • spring-context

1.pom.xml

&ltproject xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt
	&ltmodelVersion&gt4.0.0&lt/modelVersion&gt
	&ltgroupId&gtcom.practice&lt/groupId&gt
	&ltartifactId&gtInjectListDemo&lt/artifactId&gt
	&ltversion&gt0.0.1-SNAPSHOT&lt/version&gt
	&ltpackaging&gtwar&lt/packaging&gt
	&ltdependencies&gt

		&ltdependency&gt
			&ltgroupId&gtorg.springframework&lt/groupId&gt
			&ltartifactId&gtspring-core&lt/artifactId&gt
			&ltversion&gt4.3.2.RELEASE&lt/version&gt
		&lt/dependency&gt

		&ltdependency&gt
			&ltgroupId&gtorg.springframework&lt/groupId&gt
			&ltartifactId&gtspring-beans&lt/artifactId&gt
			&ltversion&gt4.3.2.RELEASE&lt/version&gt
		&lt/dependency&gt

		&ltdependency&gt
			&ltgroupId&gtorg.springframework&lt/groupId&gt
			&ltartifactId&gtspring-context&lt/artifactId&gt
			&ltversion&gt4.3.2.RELEASE&lt/version&gt
		&lt/dependency&gt


	&lt/dependencies&gt
&lt/project&gt

2.Writing Spring Beans

In This Example Im Writing one Spring Bean Named With WelcomeBean

WelcomeBean.java

package com.practice;

public class WelcomeBean {
	private String wishMessage;

	public String getWishMessage() {
		return wishMessage;
	}

	public void setWishMessage(String wishMessage) {
		this.wishMessage = wishMessage;
	}
	

}


3.Setting Up Spring Configuration File

To Enable scope ,we need to add “scope” attribute to &ltbean&gt tag in spring configuration file(applicationContext.xml).

applicationContext.xml

&ltbeans 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-4.3.xsd"&gt
	&ltbean id="welcomeBean" class="com.practice.WelcomeBean" scope ="singleton"&gt
		
	&lt/bean&gt
	
&lt/beans&gt

4.Now Create Test class ,RunThe Code

AppTest.java

package com.practice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AppTest {

	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		WelcomeBean  welcome  = (WelcomeBean)context.getBean("welcomeBean");
		welcome.setWishMessage("welcome to javalschool");
		System.out.println("Hello "+welcome.getWishMessage());
		System.out.println("----------------------");
		
		WelcomeBean welcome1 = (WelcomeBean)context.getBean("welcomeBean");
		System.out.println("Hello "+welcome1.getWishMessage());

	}

}

output:

Hello welcome to javalschool
----------------------
Hello welcome to javalschool
	
If you want to change the scope value to prototype in spring configuration File and Run the code again

applicationContext.xml

&ltbeans 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-4.3.xsd"&gt
	&ltbean id="welcomeBean" class="com.practice.WelcomeBean" scope ="prototype"&gt
		
	&lt/bean&gt
	
&lt/beans&gt

output:

Hello welcome to javalschool
----------------------
Hello welcome to null