Pages Navigation Menu

Coding is much easier than you think

Bean Scopes

 

The scope of a particular bean decides the number of Bean instance that will be returned per request. This is decided depending on the usage of a bean and inadvently influences the life cycle of the bean.

We have already seen that the scopes of Bean supported by spring are :-

 

ScopeDescription
SingletonOnly one instance of the bean will be available per Spring IOC container. By default, a bean has singleton scope.
PrototypeA new bean instance is returned, every time there is a request
RequestReturns a single bean instance per HTTP request
SessionReturns a single bean instance per HTTP session
globalSessionReturns a single bean instance per global HTTP session

 

Singleton scope

 
When a bean is a singleton, 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.

To put it another way, 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.

 

singletonBean

 
A single instance of the “€˜singletonBean”€™ is shared and injected each time it is referenced. There is only one instance of the €˜singletonBean available per IOC container.
 
A bean of singleton scope can be defined as

Or it can also be defined by



as singleton is the default scope of a bean
 

Prototype  scope

 
This results in creation of a new bean instance everytime a request is made with that particular beanId(say, using getBean(“€œbeanID”€) ) . A bean of prototype scope can be defined as

 


Or as


 

prototypeBean

 
The other 3 scopes in spring are used only when there is some web configuration involved in the related bean.

Initial web configuration for request,session,and globalSession

For Servlet 2.4+ web container, e.g. when using JSF or Struts, you need to add the following javax.servlet.ServletRequestListener to the declarations in your web application’s deployment descriptor ‘web.xml’ file.

 

 
  ...
  ..
  .
  
    
            org.springframework.web.context.request.RequestContextListener
    
  
  .
  ..
  ...

 

For servlet 2.3, provide javax.servlet.Filter implementation

 
  ..
  .
  
    requestContextFilter
    org.springframework.web.filter.RequestContextFilter
  
  
    requestContextFilter
    /*
  
  .
  ..

 

Request scope

 
With the above bean definition in place, the Spring container will create a brand new instance of the
requestBean bean using the 'requestBean' bean definition every time a HTTP request is received. The 'requestBean' bean will be effectively scoped at the HTTP request level. You can change the internal state of the instance that is created , as the other requests that are also using instances created off the back of the same 'requestBean bean definition will not be seeing these changes in state since they are particular to an individual request. Once the particular request is finished processing, the bean that is scoped to the request will be discarded.

A bean of request scope can be defined as


Session scope

 
With the above bean definition in place, the Spring container will create a brand new instance of the sessionScopeBean

bean using the 'sessionScopeBean'bean definition for the lifetime of a single HTTP Session.That is , you can change the internal state of the instance that is created , as the other HTTP Sessioninstances that are also using instances created from the same 'sessionScopeBean'bean definition will not be seeing these changes in state since they are particular to an individual HTTP Session. When the HTTP Sessionis eventually discarded, the bean that is scoped to that particular HTTP Sessionwill also be discarded.

 

A bean of session scope can be defined as


Global session scope

 
The global sessionscope is similar to the standard HTTP Sessionscope, and is used only in the context of portlet-based web applications. The portlet specification defines the notion of a global Sessionthat is shared amongst all of the various portlets that make up a single portlet web application. Beans defined at the global sessionscope are scoped (or bound) to the lifetime of the global portlet Session.

Note, if you are writing a standard Servlet-based web application and you define one or more beans as having global sessionscope, the standard HTTP Sessionscope will be used, and no error will be raised.

A bean of global session scope can be defined as