Pages Navigation Menu

Coding is much easier than you think

Bean Scopes

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 :-

 

Scope Description
Singleton Only one instance of the bean will be available per Spring IOC container. By default, a bean has singleton scope.
Prototype A new bean instance is returned, every time there is a request
Request Returns a single bean instance per HTTP request
Session Returns a single bean instance per HTTP session
globalSession Returns 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

<bean id="singletonBean" class="com.simpleCodeStuffs" scope="singleton"/>

Or it can also be defined by

<bean id="singletonBean" class="com.simpleCodeStuffs"/>

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

 

<bean id="prototypeBean" class="com.simpleCodeStuffs" singleton="false"/>

Or as

<bean id="prototypeBean" class="com.simpleCodeStuffs" scope="prototype"/>

 

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.

 

 <web-app>
  ...
  ..
  .
  <listener>
    <listener-class>
            org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>
  .
  ..
  ...
</web-app>

 

For servlet 2.3, provide javax.servlet.Filter implementation

 <web-app>
  ..
  .
  <filter> 
    <filter-name>requestContextFilter</filter-name> 
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
  </filter> 
  <filter-mapping> 
    <filter-name>requestContextFilter</filter-name> 
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  .
  ..
</web-app>

 

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

<bean id="requestBean" class="com.simpleCodeStuffs" scope="request"/>

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 Session instances 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 Session is eventually discarded, the bean that is scoped to that particular HTTP Session will also be discarded.

 

A bean of session scope can be defined as

<bean id="sessionScopeBean" class="com.simpleCodeStuffs" scope="session"/>

Global session scope

 
The global session scope is similar to the standard HTTP Session scope, and is used only 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 (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 session scope, the standard HTTP Session scope will be used, and no error will be raised.

A bean of global session scope can be defined as

<bean id="globalSessionBean" class="com.simpleCodeStuffs" 
                                        scope="globalSession"/>