Wednesday, April 9, 2014

Bean Scopes in Spring

Spring Provides Multiple types of Scopes.Here we can consider scope is availability of bean Object.The different types of scopes are
  1. singleton                                                             
  2. prototype
  3. request
  4. session
  5. globalsession

singleton: The singleton means creating only one object/ instance per JVM.
The class which allow to create only one object per JVM is known as singleton class.
  • This is the default scope of spring bean.
  • spring container returns the same object every time you access  from BeanFactory or ApplicationContext objects.

Ex for specifying scope in spring configuration file of a bean is

<bean id="beanid" class="beanclass" scope="singleton"/>
                                              or
<bean id="beanid" class="beanclass" scope="singleton">
....
...
</bean>

for singleton scope no need to specify it in spring configuration file ,because it is the default scope.

prototype: In this prototype scope the spring container creates and returns a new object for each time when you access  from BeanFactory or ApplicationContext objects.

Ex for specifying scope in spring configuration file of a bean is

<bean id="beanid" class="beanclass" scope="prototype"/>
                                              or
<bean id="beanid" class="beanclass" scope="prototype">
....
...
</bean>

request:
  • The request scoped bean object will be created one per HTTP request .
  • This bean object will become request object attribute value.
Ex for specifying scope in spring configuration file of a bean is

<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>
                                                            or
<bean id="loginAction" class="com.foo.LoginAction" scope="request">
....
...
</bean>

  • With the above bean definition  the Spring container will create a brand new instance of the LoginAction bean using the 'loginAction' bean definition for each and every HTTP request.
  • When the request is finished the bean that is scoped to the request will be discarded.
  • It is useful only in web Environment.

session:
The session scoped beans will be created one per HTTP session.
This bean object will become HTTP session scope object.

Ex for specifying scope in spring configuration file of a bean is

<bean id="loginAction" class="com.foo.LoginAction" scope="session"/>
                                                            or
<bean id="loginAction" class="com.foo.LoginAction" scope="session">
....
...
</bean>

  • With the above bean definition  the Spring container will create a brand new instance of the LoginAction bean using the 'loginAction' bean definition for each and every HTTP session.
  • When the HTTP session is finished the bean that is scoped to the session will be discarded.
  • It is useful only in web Environment.

globalsession: 
  • This is similar to the session scope.
  • But this is useful in Spring based portlet development environment.
Ex for specifying scope in spring configuration file of a bean is

<bean id="loginAction" class="com.foo.LoginAction" scope="globalsession"/>
                                                            or
<bean id="loginAction" class="com.foo.LoginAction" scope="globalsession">
....
...
</bean>

These are the scopes in Spring Framework.

0 comments:

Post a Comment