i'm new to spring and i read this :
Basically a bean has scopes which defines their existence on the application
Singleton: means single bean definition to a single object instance per Spring IOC container.
Prototype: means a single bean definition to any number of object instances.
So What is the "object instance" .
Let'just simply look this up through Code.
Following is a TennisCoach Bean with default
singleton
ScopeFollowing is a TennisCoach Bean with prototype scope
Following is a Main class :
For singleton scope the output is :
For prototype scope the output is :
Hope this answers your question. :D
Prototype scope = A new object is created each time it is injected/looked up. It will use
new SomeClass()
each time.Singleton scope = (Default) The same object is returned each time it is injected/looked up. Here it will instantiate one instance of
SomeClass
and then return it each time.See also:
Singleton: If scope is set to singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.
Prototype: If scope is set to prototype, the Spring IoC container creates new bean instance of the object every time a request for that specific bean is made. As a rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.
Prototype scope: A new object is created each time it is injected.
Singleton scope: The same object is returned each time it is injected.
Prototype scope is used for all beans that are stateful, while the singleton scope should be used for stateless beans. Let me explain with my example. Please copy and run it by yourself to get a clear understanding. Consider an interface Coach.
We have another class called TrackCoach which implements Coach.
Now there is a FortuneService interface.
It is implemented by our class HappyFortuneService.
Let's wire the two classes and inject an object bean of one class into another using Xml. Let's perform dependency injection. Note that we can do this using java annotation too.
Notice that
scope = singleton
.Now let's define our BeanScopeDemoApp, which has our main method.
Once you run the above code, you will see the following results:
It's pointing the same object and occupies the same memory location after calling it twice. Now let's change the
scope = prototype
in our Xml file, save it and run the BeanScopeDemoApp again.You will see the following results:
It's pointing the different object and occupies the different memory locations after calling it twice. This would be a graphical illustration of what i have just said.
Note : Bean with any scope will be created if it is referred by other beans and called using Application context.
Example code to check this.
This will print the relevant text when constructor is invoked.
for the below code
Bean definition is
Now I changed the scope in bean definition to singleton . Constructor is called only once during initialization of context. Next I removed the scope attribute and observed same behavior as singleton.
Adding to the above..dont get confuse with the java singleton. according to JAVA spec singleton means only one instance of that bean will be created per JVM. but in spring singleton means one instance for that particular bean will be created per application context. so if your app has more than one context you can still have more than one instance for that bean.