Issue
I want to know when should I exactly use the prototype, request and session scope in Spring? I have understood that singleton returns the same object instance if the bean is requested.
Then why should we consider other spring scopes?
Explanations with examples would help a lot to understand the need for it.
Solution
Here is an explanation of Spring bean scopes with associated examples:
Prototype
What-> it provides a different instance every time for each request
When-> your bean contains a state (i.e. object attributes) which you want to keep isolated, and you want to be sure it does not get re-used at the next request
Request
What-> it provides a different instance every time for each HTTP request, available only in the context of a Web Application
When-> your bean models an incoming entity (i.e. Product object) which you need to capture and process within that web request
Session
What-> it provides a different instance for each HTTP Session, available only in the context of a Web Application
When-> local caching associated to a specific user, for example a ShoppingBasket or a UserSession, you normally you call this a Stateful application
Answered By - Beppe C
Answer Checked By - Senaida (JavaFixing Volunteer)