Spring Bean Scope Examples - DevDummy

Latest

Views | Thoughts | Concepts | Techniques

Wednesday, August 16, 2017

Spring Bean Scope Examples



Spring framework provide enough flexibility to define the scope of the objects specifically. That means you can define the boundaries of the objects to behave at its creation.
1.     Singleton - Single object instance per Spring IoC container.
<bean id="springService" class="com.devdummy.spring.test.SpringService" />

It is not required to define the singleton scope specifically as it is the default scope.
2.     Prototype - Any number of object instances.
<bean id="springService" class="com.devdummy.spring.test.SpringService" scope="prototype"/>
3.     Request - lifecycle of a single HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
<bean id="springService" class="com.devdummy.spring.test.SpringService" scope="request"/>
4.     Session - lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
   
<bean id="springService" class="com.devdummy.spring.test.SpringService" scope="session"/>

2           5.   Global session - lifecycle of a global HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
<bean id="springService" class="com.devdummy.spring.test.SpringService" scope="globalSession"/>

This scope is only applicable for Portlet application which runs in a Portlet container. This allows the bean to scope on all the Portlets.

However this has the similar effect as session scope in Servlet based applications.