Spring Bean Scopes – Singleton and Prototype


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.

2. 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:

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;

	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"
	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 id="customer1_2" class="com.javasampleapproach.beanscope.Customer">
		<property name="name" ref="name1" />

	<!-- <<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"
		<property name="firstName" value="Jack" />
		<property name="lastName" value="Smith" />

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

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

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


4. Add commands to SpringBootApplication Class

Open SpringBeanScopeApplication.java, change the content inside:

public class SpringBeanScopeApplication {

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

		//Bean customer1
		Customer customer1_1 = (Customer) context.getBean("customer1_1");
		Name name1 = (Name) context.getBean("name1");
		Customer customer1_2 = (Customer) context.getBean("customer1_2");

		//Bean customer2
		Customer customer2_1 = (Customer) context.getBean("customer2_1");
		Name name2 = (Name) context.getBean("name2");
		Customer customer2_2 = (Customer) context.getBean("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


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 *