Hi I got this asked in an interview question paper.
Singleton and Prototype (non-singleton) which is stateful and which is stateless
I am not sure if prototype is stateless ? Is there any problem with the question ?
The question itself is poorly worded. You can have state in both Singletons and Prototypes (instances), as in it is legal code, but you do not need to have state in either cases. Since Spring is mentioned, I will try to answer this in regards to working with Spring.
In terms of Spring bean scope, singleton
will cause the ApplicationContext
to create a single instance and use that instance everywhere the bean is asked for. prototype
will cause the ApplicationContext
to create a new instance each time the bean is asked for.
It is ok for both of these to be stateful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With