Spring Bean Scopes – Singleton and Prototype

spring-framework-bean-scopes-singleton-prototype-feature-image

Spring Bean Scopes helps you control the scope of the objects which are created from bean definition. This approach is wonderful because you can choose the scope by configuring bean instead of working at Java class level.

The Spring Framework supports 7 scopes, but this tutorial introduces you 2 of them: singleton scope and prototype scope. The others will be discussed in next article.

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE

II. Overview

1. Goal

To build a simple application that shows you the way singleton scope and prototype scope act and the difference between them.
springbeanscope-xgoal

2. Project Structure

springbeanscope-project-structure
Customer and Name are Classes for Java Objects.
SpringBeanScopeApplication is SpringBootApplication that includes function main to get Beans and run commands to illustrate example.
bean.xml contains all important bean configuration.

3. Step to do

– Create Spring Boot project
– Create DataModels
– Create Spring Bean Configuration File
– Add commands to SpringBootApplication Class
– Run Spring Boot Application & Enjoy Result

III. Practice

1. Create Spring Boot project

Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields:
springbeanscope-startproj

Click Next and Finish. Spring Boot project will be created successfully.

2. Create DataModels

Under package model, create class Name and CustomerName.java


public class Name {
	private String firstName;
	private String lastName;

	public Name() {
	}
	
	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}

Content of Customer.java:


public class Customer {
	private Name name;

	public Name getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "Customer: " + this.getName().getFirstName() + " "
				+ this.getName().getLastName();
	}
}

3.Create Spring Bean Configuration File

Under src/main/resources, create bean.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-4.3.xsd">

	<bean id="customer1_1" class="com.javasampleapproach.beanscope.Customer">
		<property name="name" ref="name1" />
	</bean>

	<bean id="customer1_2" class="com.javasampleapproach.beanscope.Customer">
		<property name="name" ref="name1" />
	</bean>

	<!-- <<bean id="name1" class="com.javasampleapproach.beanscope.Name"> The 
		following is equivalent, though redundant (singleton scope is the default) -->
	<bean id="name1" class="com.javasampleapproach.beanscope.Name"
		scope="singleton">
		<property name="firstName" value="Jack" />
		<property name="lastName" value="Smith" />
	</bean>

	<bean id="customer2_1" class="com.javasampleapproach.beanscope.Customer">
		<property name="name" ref="name2" />
	</bean>

	<bean id="customer2_2" class="com.javasampleapproach.beanscope.Customer">
		<property name="name" ref="name2" />
	</bean>

	<bean id="name2" class="com.javasampleapproach.beanscope.Name"
		scope="prototype">
		<property name="firstName" value="Adam" />
		<property name="lastName" value="Johnson" />
	</bean>

</beans>

4. Add commands to SpringBootApplication Class

Open SpringBeanScopeApplication.java, change the content inside:


@SpringBootApplication
public class SpringBeanScopeApplication {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

		//Bean customer1
		Customer customer1_1 = (Customer) context.getBean("customer1_1");
		System.out.println(customer1_1);
		
		Name name1 = (Name) context.getBean("name1");
		name1.setFirstName("Peter");
		name1.setLastName("Pan");
		
		Customer customer1_2 = (Customer) context.getBean("customer1_2");
		System.out.println(customer1_2);

		//Bean customer2
		Customer customer2_1 = (Customer) context.getBean("customer2_1");
		System.out.println(customer2_1);
		
		Name name2 = (Name) context.getBean("name2");
		name2.setFirstName("Peter");
		name2.setLastName("Pan");
		
		Customer customer2_2 = (Customer) context.getBean("customer2_2");
		System.out.println(customer2_2);
	}
}

5. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results in Console Screen:


Customer: Jack Smith
Customer: Peter Pan
Customer: Adam Johnson
Customer: Adam Johnson

IV. Source Code

spring-beanscope



By grokonez | September 8, 2016.

Last updated on April 28, 2021.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*