In the Spring Framework, Singleton and Prototype bean scopes define how bean instances are created and managed by the Spring IoC container.
- Bean scope defines how many objects of a bean are created in the Spring container.
- Singleton creates only one shared instance of a bean.
- Prototype creates a new instance every time the bean is requested.
Singleton Scope
The Singleton scope is the default scope in Spring. In this scope, the Spring container creates only one instance of the bean and shares it across the entire application.
- The same object is shared across all components.
- Created when the application context starts (by default).
- Suitable for stateless services such as service classes and repositories
Steps to Create Singleton Scope
Step 1: Create a Maven Project
- Open IntelliJ IDEA -> New Project
- Select Maven
- Choose your JDK
- Project Name: SpringSingletonDemo
- Click Finish
Step 2: Create Package Structure

Step 3: Add Spring Dependency (pom.xml)
Add the Spring Context dependency to enable Spring IoC.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.30</version>
</dependency>
</dependencies>
Explanation:
- The name field stores data for the bean.
- setName(): allows you to set the value of name (used in your client).
- getName(): allows you to retrieve the value of name.
Step 4: Create Bean Class (HelloWorld.java)
package bean;
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
// Getter method
public String getName() {
return name;
}
}
Step 5: Create Spring XML Configuration File
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- Singleton bean configuration -->
<bean id="hw"
class="bean.HelloWorld"
scope="singleton" />
</beans>
Explanation:
- <bean> tag registers HelloWorld with the Spring IoC container.
- id="hw" is the unique identifier for this bean.
- class="bean.HelloWorld" tells Spring which class to instantiate.
- scope="singleton" specifies that Spring will create only one instance of this bean and reuse it every time it’s requested.
- This XML acts as a map for the Spring container, so Spring knows how to manage the bean lifecycle.
Step 6: Create Client Class (Client.java)
package driver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.HelloWorld;
public class Client {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
// First request
HelloWorld obj1 = (HelloWorld) context.getBean("hw");
obj1.setName("Geeks1");
System.out.println("Hello object (hello1) Your name is: " + obj1.getName());
// Second request
HelloWorld obj2 = (HelloWorld) context.getBean("hw");
System.out.println("Hello object (hello2) Your name is: " + obj2.getName());
// Compare references
System.out.println("'obj1' and 'obj2' refer to same object: " + (obj1 == obj2));
// Print memory addresses
System.out.println("Address of obj1: " + obj1);
System.out.println("Address of obj2: " + obj2);
}
}
Explanation:
- ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"): This line loads the Spring IoC container and reads spring.xml to know which beans to create.
- HelloWorld Geeks1 = (HelloWorld) context.getBean("hw"): Requests the bean for the first time. Spring creates the singleton instance now.
- Geeks1.setName("Geeks1"): Sets the name property of the singleton bean.
- HelloWorld Geeks2 = (HelloWorld) context.getBean("hw"): Requests the bean a second time. Spring returns the same instance created earlier.
- System.out.println(Geeks1.getName()); and System.out.println(Geeks2.getName()): Both print "Geeks1" because there is only one object shared between the two references.
Step 7: Run the Application
- Right-click on Client.java
- Click Run
Output:

Prototype Scope
In the Prototype scope, the Spring container creates a new bean instance every time it is requested from the container. Unlike singleton, each request gets a completely new object.
- Multiple objects are created.
- A new instance is returned every time getBean() is called.
- The container manages creation only, not the full lifecycle.
Steps to Create Prototype Scope
Step 1: Create a New Project
- Open IntelliJ IDEA -> Click New Project.
- Choose Java -> Maven (optional, but recommended for dependencies).
- Give your project a name, e.g., SpringBeanScopesDemo.
- Finish and create the project.
Step 2: Create Package Structure

Step 3: Add Spring Dependencies
Open pom.xml and add the following dependencies:
<dependencies>
<!-- Spring Context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.30</version>
</dependency>
</dependencies>
Step 4: Create the Bean Class(HelloWorld.java)
package bean;
public class HelloWorld {
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
Explanation:
- This is your bean class.
- It will be managed by Spring IoC container.
- name property can be used to demonstrate state.
Step 5: Create Spring XML Configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- Singleton bean -->
<bean id="singletonBean" class="bean.HelloWorld" scope="singleton"/>
<!-- Prototype bean -->
<bean id="prototypeBean" class="bean.HelloWorld" scope="prototype"/>
</beans>
Explanation:
- singletonBean -> Only one instance shared by all requests.
- prototypeBean -> New instance created for every request.
Step 6: Create Client Class
package driver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.HelloWorld;
public class Client {
public static void main(String[] args) {
// Load Spring container
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
// --- Singleton Example ---
HelloWorld singleton1 = (HelloWorld) context.getBean("singletonBean");
singleton1.setName("Singleton One");
HelloWorld singleton2 = (HelloWorld) context.getBean("singletonBean");
System.out.println("Singleton1 Name: " + singleton1.getName());
System.out.println("Singleton2 Name: " + singleton2.getName());
System.out.println("Are both singleton objects same? " + (singleton1 == singleton2));
// --- Prototype Example ---
HelloWorld prototype1 = (HelloWorld) context.getBean("prototypeBean");
prototype1.setName("Prototype One");
HelloWorld prototype2 = (HelloWorld) context.getBean("prototypeBean");
System.out.println("Prototype1 Name: " + prototype1.getName());
System.out.println("Prototype2 Name: " + prototype2.getName());
System.out.println("Are both prototype objects same? " + (prototype1 == prototype2));
}
}
Explanation:
- singletonBean -> Both references share the same object.
- prototypeBean -> Each reference is a new object.
- Output will clearly show the difference in state and reference.
Step 7: Run the Program
- Right-click on Client.java -> Run 'Client.main()'.
Output:

Singleton Vs Prototype Bean
Feature | Singleton | Prototype |
|---|---|---|
Instance Creation | One instance per container | New instance per request |
Object Sharing | Same object shared | Different object each time |
Default Scope | Yes | No |
Explicit Declaration | Not required | Required |
Best Suited | Stateless beans | Stateful beans |