Thursday, 14 August 2014

Different between Singleton Scope and Prototype Scope in spring

1. Singleton means single bean instance per IoC container, and prototype means any number of object instances per IoC container. The default scope is "singleton".
2. Singleton scope is used for stateless object use. For example, injectiong a DAO (i.e. Data Access Object) into a service object. DAOs don't need to maintain conversation state.
3. Prototype is useful when your objects maintain state in a multi-threaded environment. Each thread needs to use its own object and cannot share the single object.
4. prototype scope allows the bean to be instantiated whenever it is requested. Every time a separate instance is created, just opposite to singleton. Stateful beans which hold the conversational state should be declared as prototype scope.

No comments:

Post a Comment