Singleton and Prototype Bean Scopes in Spring

Zahidul Islam
2 min readOct 1, 2023

--

Introduction

In the Spring Framework, beans serve as the fundamental units of an application. A bean’s scope determines its life cycle and how it’s handled by the Spring container.

Spring offers six types of bean scopes for managing the creation, storage, and sharing of beans within an application context. Additionally, custom bean scopes can be defined. Among these scopes, four are exclusively applicable when using a web-aware Application-Context, while the remaining two namely singleton and prototype are accessible in any type of IOC container. In this discussion, we will focus on these two scopes which are available in any IOC container.

1. Singleton (default) scope

The singleton is the default bean scope in the spring container. It tells the container to create and manage only one bean class instance throughout the application context’s life cycle. 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 instance.

Java Configuration:

@Configuration
public class SingletonBeanScope {

@Bean
@Scope("singleton")
public class MyBean {
System.out.println("A new Bean instance is created!");

return new MyBean();
}
}

XML Configuration:

<bean id="mySingletonBean" class="com.zahid.MyBean" scope="singleton" />

//or
<!-- default scope -->
<bean id="mySingletonBean" class="com.zahid.MyBean" />

To confirm the singleton behavior, we can create multiple beans and compare their hashcodes. Whenever the bean creation code is run, the same hashcode will consistently be displayed.

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SingletonBeanScope.class);
context.refresh();

MyBean instance1 = context.getBean(MyBean.class);
System.out.println("Hashcode of Bean instance1: " + instance1.hashCode());

MyBean instance2 = context.getBean(MyBean.class);
System.out.println("Hashcode of Bean instance2: " + instance2.hashCode());

context.close();

The program output should look like this.

A new Bean instance is created!

Hashcode of Bean instance1: 324759237
Hashcode of Bean instance2: 324759237

2. Prototype Scope

In the prototype scope, Spring creates a new instance of the bean every time it is requested. This means that multiple instances of the prototype bean can coexist in the application context, and each client requesting the bean gets a unique instance.

Additionally, Spring does not manage the complete life-cycle of a prototype bean. We should know that destruction bean life-cycle methods are not called prototype scoped beans, only initialization callback methods are called.

Java Configuration:

@Bean
@Scope("prototype")
public class PrototypeBean {
System.out.println("A new Bean instance is created!");

return new PrototypeBean();
}

XML Configuration:

<bean id="myPrototypeBean" class="com.zahid.PrototypeBean" scope="prototype" />

Now if run the previous example code again, we will see that the bean was created twice with a different hashcode.

A new Bean instance is created!
A new Bean instance is created!

324759237
756132742

Conclusion

In this article, we have focused on the two scopes that are accessible within an IOC container. The choice of bean scope depends on the specific requirements of your application. Singleton is the default and is suitable for most cases, but prototype and other scopes can be useful when you need different instances of a bean with a different state or when working in a web environment where request and session scopes are relevant. Custom scopes can be used for more specialized scenarios.

--

--

Zahidul Islam
0 Followers

I'm a passionate software engineer and tech enthusiast, driven by a love for coding and a knack for problem-solving.