Spring Singleton Beans

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We all know how we define singleton beans in spring framework.Maybe we all have used singleton beans umpteen number of times while developing applications. But have we really understood spring singleton beans?

Spring Singleton Beans:-

We know the design patters and Singleton Design Pattern is widely known design pattern. When you implement singleton design pattern you are sure that you will have a one and only one instance of a class per classloader.

So does spring framework really implement this? Can we have multiple instances of Spring Singleton Beans? We need to find the answer with a small example. Here is the scenario.

 

Spring Singleton Beans Example:-

Consider we have two POJO classes, Department and Employee and Employee HAS-A Department. We are going to declare two Department beans and 4 Employee beans in spring-beans.xml. Consider below classes and beans file.

Department.java

Employee.java

spring-beans.xml

Now again, what is our understanding of singleton? When there is one and only one object in then it is singleton, right? So as per above bean definitions, we are defining two spring singleton beans for Department and four spring singleton beans for Employee. So above scenario cannot be correct right? Let us try to write some sample code and access these beans.

This spring singleton beans example prints below.

Employee [name=prasad, dept=Department [name=it]]
Employee [name=supriya, dept=Department [name=it]]
Employee [name=harish, dept=Department [name=finance]]
Employee [name=pranil, dept=Department [name=finance]]

So what is happening here? Everything is perfectly fine, why? Why isn’t singleton behaving the way as we expect it to be? Well the answer is below

 

Spring singleton beans are different that singleton design pattern.

This is what spring official documentation has to say about them.

The scope of the Spring singleton is best described as per container and per bean. This means that if you define one bean for a particular class in a single Spring container, then the Spring container creates one and only one instance of the class defined by that bean definition

So, in our example, you can notice that Department it is being referred by prasad and supriya, and finance is being referred by pranil and harish. So in short,

  • There is a singleton bean with id it and another one finance which are of Department type.
  • There are two Employee type beans i.e. supriya and prasad which has reference to it. 
  • There are two more Employee type beans i.e. harish and pranil, which refer to a singleton bean finance.

Diagram below will give a better understanding:

singleton

  • One bean is defined with id it  so there is only one instance for a particular id. it is being referred by two employees supriya and prasad, similar case with finance. 
  • So a single bean referred by it is returned everytime an Employee bean is created with ref=”it”.

So Spring Singleton Beans are one instance per bean definition.

I hope this article helped understand spring singleton beans.

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

2 thoughts on “Spring Singleton Beans

  • September 21, 2016 at 11:12 am
    Permalink

    pls add spring tutorial

    Reply
  • August 9, 2018 at 2:18 pm
    Permalink

    This was really helpful. Thanks much.

    Reply

Leave a Reply

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