Spring Singleton – Beans Loaded Multiple Times

1.Overview

Only one shared instance of a singleton bean is managed, and all requests for beans with an id or ids matching that bean definition result in that one specific bean instance being returned by the Spring container.

(Default) Scopes a single bean definition to a single object instance per Spring IoC container. – Spring Reference Document

But, I found some of the questions on StackOverflow like Beans loaded twice, Singleton constructor called multiple times. There are many cases that beans loaded twice or multiple times (At least we see that constructor calls). In this post, we will see one such example.

2. Singleton Constructor Called Multiple Times

See the below classes

Parent.java

Child1.java

Child2.java

We will get the below output when we run the Spring Main App (Assuming that we already configured XML/ Annotation with component-scan enabled).

As we can see here, Parent Class Constructor called 3 times. What had happened? As per Spring Singleton, Only one shared instance of a singleton bean is managed. 

3. Why did Parent Class Constructor call Multiple Times?

First call – while spring do the component scan and instantiate the Parent bean (Make note that this is not an abstract/ template class or interface)

Second and Third calls – while spring do the component scan and instantiate the Child1 and Child2 beans

Second and third calls are because subclass always calls the constructor of its superclassWhile the spring instantiates Child1 class which is the subclass of Parent, it calls Parent class constructor. Same in the case of Child2.

As per Spring reference document –it is important (at least for singleton beans) that if you have a (parent) bean definition which you intend to use only as a template, and this definition specifies a class, you must make sure to set the abstract attribute to true, otherwise the application context will actually (attempt to) pre-instantiate the abstract bean. (This is what happens for the first constructor call). Check the reference link here for more details.

4. What to do now?

As per Spring reference, we can make the Parent class as abstract like below.

Now, When we run the Main App, we will see below output.

Still, the parent class constructor called twice. Nothing wrong as per Java. But, We still not achieved the Spring Singleton.

In this case, it is better to code to Interface to get the benefits of Spring. Convert the Parent class to the interface and provide the implementation to the Child1 and Child2.

The purpose of Spring dependency injection is not just to hide the implementation class. The main point is you should not have to worry about which implementation class to use. We put the responsibility of providing the class to Spring’s container.

5. Conclusion

We have discussed why the constructor of singleton bean called multiple times. And, the preferred way of creating beans on Spring.

Thank you and please leave your comments.

References:

https://docs.oracle.com/javase/tutorial/java/IandI/super.html

https://github.com/spring-projects/spring-framework/blob/master/src/docs/asciidoc/core/core-beans.adoc#bean-definition-inheritance

https://martinfowler.com/articles/injection.html

https://stackoverflow.com/questions/7921484/spring-singleton-created-multiple-times

https://stackoverflow.com/questions/10790120/spring-singleton-being-called-twice

harinathk
 

Click Here to Leave a Comment Below 0 comments