Although Spring manages the beans for us. However, it also provides five scopes by which we can manage the behavior of our application.
There are five scopes that spring provides to configure a bean :
1. Singleton
2. Prototype
3. Session
4. Request
5. Global
Whenever we request Spring to create a bean :
Instead of creating a new object/bean every time, it returns the same object/bean for reuse. In simple words we are using the same object/bean every time we request for a new bean.
Say, for example we have requested for two 'Car' objects/beans, 'car1' and 'car2' from Spring :
For the above request, Spring will be creating a single object/bean and 'car1' and 'car2' would refer to that.
The only change we have to make is in the <bean ..> tag of 'application-context.xml'.
So, the 'application-context.xml' looks like :
Prototype is just the opposite of singleton. i.e. Every time a bean is requested, a new instance is created and handed over.
Say, for example we have requested for two 'Car' objects/beans, 'car1' and 'car2' from Spring :
For the above request, Spring will be creating two objects/beans, i.e. 'car1' and 'car2'.
The only change we have to make is in the <bean..> tag of 'application-context.xml'.
So, the 'application-context.xml' looks like :
It is used in Web Application. The scope of the bean is valid per HTTP request.
Also used in Web Applications. The scope of the bean is valid per HTTP session.