Today someone asked me, how would Spring behave with respect to object instantiation if a Prototype bean was injected into a Singleton.
This took me by surprise and the reaction was "Why in the world would we even do that?". Do we ever have instance level objects in Singletons, that might be prone to change, as that is what prototype beans would be for? No we do not, then why would we do that in Spring.
The very intent of Singleton is to embody the logic of our application code, that has no state. It makes sense for Prototype beans, to have Singleton beans injected in them, for doing work.
But the other way round, not so much.
However, I went through the spring documentation and to my surprise there was a section(3.5.3) dedicated to Prototype beans injected into Singleton beans. And as expected, though it does not fail, such a dependency injection just results in a fixed instance of the Prototype bean to be injected into the Singleton. That instance does not change through the life of the container. Essentially, a singleton instance. However, if we have three Singletons with same Prototype bean injected, it would result in three separate Prototype instance to be created and injected into each.
This makes perfect sense. Singletons are meant for multi-threaded access of logic and if the prototype injection resulted into different objects, the entire thing will go for a toss.
So what's the practical application? Even if we need different instances of a bean to be injected into our three Singletons, with different initial data, that does not require Prototype bean scope. Remember, Spring creates Singleton instance per bean definition.
So if you know any real scenario where this might be applicable, please do comment below. But for now, let's deem it as an anti-pattern. This is something Spring should shun from documentation unless that's there with the explicit intent to confuse.
Extending this to Session scope beans.
Shunning my inherent hatred towards Spring web, let's dwell into this concept. Some may consider the standard use case of session scoped beans injected into Controller or Service beans as analogous to prototype beans injected into Singleton beans.
But nothing is far from truth. Though, the session scoped bean may look like injected into the Singleton Controller or Service, it is not. What is injected is an aop-proxy, which is actually a single instance object, which fetches the corresponding session scope bean instance and returns it. This is more analogous to lookup-method injection, into singletons.
This took me by surprise and the reaction was "Why in the world would we even do that?". Do we ever have instance level objects in Singletons, that might be prone to change, as that is what prototype beans would be for? No we do not, then why would we do that in Spring.
The very intent of Singleton is to embody the logic of our application code, that has no state. It makes sense for Prototype beans, to have Singleton beans injected in them, for doing work.
But the other way round, not so much.
However, I went through the spring documentation and to my surprise there was a section(3.5.3) dedicated to Prototype beans injected into Singleton beans. And as expected, though it does not fail, such a dependency injection just results in a fixed instance of the Prototype bean to be injected into the Singleton. That instance does not change through the life of the container. Essentially, a singleton instance. However, if we have three Singletons with same Prototype bean injected, it would result in three separate Prototype instance to be created and injected into each.
This makes perfect sense. Singletons are meant for multi-threaded access of logic and if the prototype injection resulted into different objects, the entire thing will go for a toss.
So what's the practical application? Even if we need different instances of a bean to be injected into our three Singletons, with different initial data, that does not require Prototype bean scope. Remember, Spring creates Singleton instance per bean definition.
So if you know any real scenario where this might be applicable, please do comment below. But for now, let's deem it as an anti-pattern. This is something Spring should shun from documentation unless that's there with the explicit intent to confuse.
Extending this to Session scope beans.
Shunning my inherent hatred towards Spring web, let's dwell into this concept. Some may consider the standard use case of session scoped beans injected into Controller or Service beans as analogous to prototype beans injected into Singleton beans.
But nothing is far from truth. Though, the session scoped bean may look like injected into the Singleton Controller or Service, it is not. What is injected is an aop-proxy, which is actually a single instance object, which fetches the corresponding session scope bean instance and returns it. This is more analogous to lookup-method injection, into singletons.
Comments
Post a Comment