Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real World use case of bean scopes

Tags:

spring

I am learning Spring, I learned about bean scopes - what are the real world use cases for each of them, I am not able to get any help. please help when to use Singleton, Prototype , Request and Session scopes in Spring.

like image 988
user3814659 Avatar asked Sep 08 '16 21:09

user3814659


People also ask

What is the use of bean scope?

The Request Bean Scope The request scope is applicable to beans of Web aware applications. This scope defines a single bean definition that lives within a single HTTP request. This means every HTTP request will have its own instance of a bean.

What is the use case for prototype scope in Spring?

The Prototype Scope Now, if you are dealing with multiple instance requests for the object every time the user requests that specific bean, in this case, the Spring IoC container creates a new bean instance for the object every time a new request is made. One can use the prototype scope for all state-full beans.

In which scenario we will use singleton and prototype scope?

As a rule of thumb, you should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans.

What are the possible bean scopes present in Spring?

Bean Scopes in Spring The spring framework provides five scopes for a bean. We can use three of them only in the context of web-aware Spring ApplicationContext and the rest of the two is available for both IoC container and Spring-MVC container.


1 Answers

  • Singleton: It returns a single bean instance per Spring IoC container.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. If no bean scope is specified in the configuration file, singleton is default. Real world example: connection to a database

  • Prototype: It returns a new bean instance each time it is requested. It does not store any cache version like singleton. Real world example: declare configured form elements (a textbox configured to validate names, e-mail addresses for example) and get "living" instances of them for every form being created

  • Request: It returns a single bean instance per HTTP request. Real world example: information that should only be valid on one page like the result of a search or the confirmation of an order. The bean will be valid until the page is reloaded.

  • Session: It returns a single bean instance per HTTP session (User level session). Real world example: to hold authentication information getting invalidated when the session is closed (by timeout or logout). You can store other user information that you don't want to reload with every request here as well.

  • GlobalSession: It returns a single bean instance per global HTTP session. It is only valid in the context of a web-aware Spring ApplicationContext (Application level session). It is similar to the Session scope and really only makes sense in the context of portlet-based web applications. The portlet specification defines the notion of a global Session that is shared among all of the various portlets that make up a single portlet web application. Beans defined at the global session scope are bound to the lifetime of the global portlet Session.

like image 70
Irina Avram Avatar answered Oct 28 '22 13:10

Irina Avram