Search This Blog

Monday, 13 February 2017

Bean scopes

Bean scopes in Spring

When we create a bean definition, we are actually creating is actual instances of the class defined by that bean definition. You can control the various dependencies and configuration values along with the scope of the objects created from a particular bean definition. Beans can be defined to be deployed in one of a number of scopes: out of the box, the Spring Framework supports five scopes of which three are available only if you are using a web-aware ApplicationContext.


Scope
Description
singleton
A single bean definition to a single object instance per Spring IoC container.
prototype
A single bean definition to any number of object instances.
request
A single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
session
A single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
global session
A single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.


The singleton scope
The singleton scope is the default scope in Spring. For a singleton bean, only one shared instance of the bean will be managed, and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned by the Spring container.
When you define a bean definition and it is scoped as a singleton, then the Spring IoC container will create exactly one instance of the object defined by that bean definition. This single instance will be stored in a cache of such singleton beans, and all subsequent requests and references for that named bean will result in the cached object being returned.

The prototype scope

Prototype scope of bean will create a new bean instance every time a request for that specific bean is made. You should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans.
When deploying a bean in the prototype scope, please take note that , Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, decorates and otherwise assembles a prototype object, hands it to the client and then has no further knowledge of that prototype instance. This means that while initialization lifecycle callback methods will be called on all objects regardless of scope, in the case of prototypes, any configured destruction lifecycle callbacks will not be called. It is the responsibility of the client code to clean up prototype scoped objects and release any expensive resources that the prototype bean(s) are holding onto.

The request scope

The Spring container will create a brand new instance of the bean definition for each and every HTTP request. You can change the internal state of the instance that is created as much as you want. When the request is finished processing, the bean that is scoped to the request will be discarded.

The session scope

The Spring container will create a brand new instance of the  bean using the bean definition for the lifetime  of a single HTTP Session. In other words, the bean will be effectively scoped at the HTTP Session level. Just like request-scoped beans, you can change the internal state of the instance that is created. When the HTTP Session is eventually discarded, the bean that is scoped to that particular HTTP Session will also be discarded.

The global session scope

The global session scope is similar to the HTTP Session scope, and makes sense in the context of portlet-based web applications. The portlet specification defines the notion of a global Session that is shared amongst all of the various portlets that make up a single portlet web application. Beans defined at the global session scope are scoped to the lifetime of the global portlet Session.


Bean scopes
Bean scopes

No comments:

Post a Comment