Lesson

Singleton, Prototype Scope

spring/Spring Boot

Singleton and Prototype Bean Scopes

1. Introduction

In Spring, bean scope defines how many objects of a bean are created and managed by the IoC container.

Main scopes:

  • Singleton

  • Prototype


2. Singleton Scope

Singleton is the default scope in Spring.

Key Features

  • Only one instance is created

  • Same object is shared across the application

  • Created at container startup (by default)

  • Best for stateless beans


3. Example of Singleton Scope

Step 1: Add Dependency

plaintext
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.30</version>
</dependency>

Step 2: Create Bean Class

plaintext
package bean;

public class HelloWorld {

    private String name;

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

    public String getName() {
        return name;
    }
}

Step 3: Configure XML

plaintext
<bean id="hw"
      class="bean.HelloWorld"
      scope="singleton"/>

Step 4: Client Code

plaintext
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");

        HelloWorld obj1 = (HelloWorld) context.getBean("hw");
        obj1.setName("Anil");

        HelloWorld obj2 = (HelloWorld) context.getBean("hw");

        System.out.println(obj1.getName());
        System.out.println(obj2.getName());

        System.out.println(obj1 == obj2);
    }
}

Output Understanding

  • Both objects are same

  • Changes in one object reflect in another


4. Prototype Scope

Prototype scope creates a new object every time the bean is requested.

Key Features

  • Multiple instances are created

  • New object for every getBean() call

  • Container manages creation only

  • Best for stateful beans


5. Example of Prototype Scope

XML Configuration

plaintext
<bean id="prototypeBean"
      class="bean.HelloWorld"
      scope="prototype"/>

Client Code

plaintext
HelloWorld obj1 = (HelloWorld) context.getBean("prototypeBean");
obj1.setName("First");

HelloWorld obj2 = (HelloWorld) context.getBean("prototypeBean");

System.out.println(obj1.getName());
System.out.println(obj2.getName());

System.out.println(obj1 == obj2);

Output Understanding

  • Objects are different

  • Changes are not shared


6. Singleton vs Prototype

Instance Creation

  • Singleton: One object per container

  • Prototype: New object every time

Object Sharing

  • Singleton: Shared

  • Prototype: Not shared

Default Scope

  • Singleton: Yes

  • Prototype: No

Usage

  • Singleton: Stateless beans

  • Prototype: Stateful beans

7. Conclusion

  • Singleton is used in most applications

  • Prototype is used when you need multiple objects

  • Choose scope based on application requirement