<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div style="color: #274e13;">In this article we will discuss about Spring bean scopes. In Spring application, we get the bean from the <b><a href="https://dineshonjava.com/spring-ioc-container/">Spring container</a> </b>with some default configuration. <b>Default behavior is that every beans in the <a href="https://dineshonjava.com/spring-ioc-container/">Spring container</a> are initialized when <a href="https://dineshonjava.com/what-is-bean-factory-in-spring/">bean configuration file</a> loaded to the <a href="https://dineshonjava.com/java-virtual-machine/">JVM</a></b>. Whenever <b>getBean </b>is called container recognized the <b>bean by given bean id and return that bean to the caller</b>. <b>One more default behavior is that every beans has only one instance in the <a href="https://dineshonjava.com/spring-ioc-container/">spring container</a>.</b></div>
<h2>Spring Bean Scopes</h2>
<div style="color: #274e13;">
<p>Spring Bean Scopes means which is used to decide which type of bean instance should be return from <b><a href="https://dineshonjava.com/spring-ioc-container/">Spring container</a> back to the caller.</b> Basic Spring Bean Scopes are only two types-</p>
<h3><b>1. Singleton:(Default)</b></h3>
<p>Scopes a single bean definition to a single object instance per Spring IoC container.</p>
<h3><b>2. Prototype</b>:</h3>
<p>Scopes a single bean definition to any number of object instances.</p>
<p>But at web environment spring have three more scopes as following:</p>
<h3><b>Request Scope</b>:</h3>
<p>This scopes a bean definition to an HTTP request.</p>
<h3><b>Session Scope</b>:</h3>
<p>This scopes a bean definition to an HTTP session.</p>
<h3><b>Global Session</b>:</h3>
<p>Scopes a single bean definition to the lifecycle of a global HTTP Session.</p>
<p><b> </b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/06/Spring-Bean-Scopes.jpg" width="640" height="400" border="0" /></div>
<p><b> </b></p>
</div>
<div style="color: #274e13;">
<p>We can control not only the various <b><a href="https://dineshonjava.com/dependency-injection-in-spring/">dependencies and configuration</a></b> values that are to be plugged into an object that is created from a particular bean definition, but also the <i class="firstterm">scope</i> of the objects created from a particular bean definition. This approach is very powerful and gives you the flexibility to <i>choose</i> the scope of the objects you create through configuration.</p>
</div>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<div></div>
<div style="color: #274e13;"><b>Basic Spring Bean Scopes </b>are only two types are given below.</div>
<ol style="text-align: left;">
<li style="color: #274e13;"><b><a href="https://dineshonjava.com/singleton-bean-scope-in-spring/">Singleton Bean Scope</a> &#8211; </b>Return a single bean instance per <a href="https://dineshonjava.com/spring-ioc-container/">Spring IoC container</a></li>
<li><b><a href="https://dineshonjava.com/prototype-bean-scope-with-annotation/">Prototype Bean Scope</a> &#8211; </b>Return a new bean instance each time when requested</li>
</ol>
</div>
<div style="color: #274e13;">
<h2><b>1. <a href="https://dineshonjava.com/singleton-bean-scope-in-spring/">Singleton Bean Scope</a>: </b></h2>
<p>Scopes a single bean definition to a single object instance per <a href="https://dineshonjava.com/spring-ioc-container/">Spring IoC container</a>. This is the default behavior of the <a href="https://dineshonjava.com/spring-ioc-container/">spring container</a>.</p>
</div>
<div style="color: #274e13;">
<p>When a bean is a <b>singleton</b>, only one <i>shared</i> instance of the bean will be managed, and all requests for beans with an<i><b> id or ids</b></i> matching that bean definition will result in that one specific bean instance being returned by the <a href="https://dineshonjava.com/spring-ioc-container/">Spring container</a>.</p>
</div>
<div style="color: #274e13;">
<p>We can say another way, <b>when you define a bean definition and it is scoped as a singleton,</b> then the <a href="https://dineshonjava.com/spring-ioc-container/">Spring IoC container</a> will create <b><i>exactly one</i> <i>instance </i></b>of the object defined by that bean definition. This single instance will be stored in a cache of such singleton beans, and <b><i>all subsequent requests and references</i> </b>for that named bean will result in the <b>cached object being returned</b>.</p>
<p>To define a singleton scope, you can set the <b>scope</b> property to <b>singleton</b> in the bean configuration file, as shown below:</p>
</div>
</div>
<pre class="highlight"><;bean class="com.dineshonjava.sdnext.beanscope.Point" id="zeroPoint" scope="singleton">; 
 <;property name="x" value="0">;<;/property>; 
 <;property name="y" value="0">;<;/property>; 
<;/bean>; 
</pre>
<p><b><a href="https://dineshonjava.com/singleton-bean-scope-in-spring/">See the singleton scope of bean with full example.</a></b></p>
<div style="color: #274e13;"><b>Using Annotation for Bean Scope:</b></div>
<pre class="highlight">@Service 
@Scope("singleton") 
public class Point 
{ 
 private int x; 
 private int y; 
 
 public void setX(int x){ 
 this.x = x; 
 } 
 
 public void setY(int y){ 
 this.y = y; 
 } 
} 
</pre>
<p><b><a href="https://dineshonjava.com/singleton-bean-scope-in-spring/">See the singleton scope of bean with full example.</a></b><br />
<b style="color: blue;">NOTE</b> : This singleton is differ from the singleton pattern in Java Class. <b>Single pattern in java mean you can create the only one instance of a that class in <a href="https://dineshonjava.com/java-virtual-machine/">JVM</a></b>. <b>But In spring singleton bean scope means every container can create only single bean in the <a href="https://dineshonjava.com/spring-ioc-container/">Spring IoC Container</a> but a <a href="https://dineshonjava.com/java-virtual-machine/">JVM </a>can have multiple <a href="https://dineshonjava.com/spring-ioc-container/">Spring IoC Container</a> so <a href="https://dineshonjava.com/java-virtual-machine/">JVM </a>can multiple beans rather than bean singleton bean scope.</b></p>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/06/singleton.jpg" width="640" height="324" border="0" /></div>
<h2><b>2. <a href="https://dineshonjava.com/prototype-bean-scope-with-annotation">Prototype Bean Scope</a>: </b></h2>
<p>If scope is set to prototype, the <a href="https://dineshonjava.com/spring-ioc-container/">Spring IoC container</a> creates new bean instance of the object every time a request for that specific bean is made. <b>As a rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.</b><br />
<b><br />
</b> To define a prototype scope, you can set the <b>scope</b> property to <b>prototype</b> in the bean configuration file, as shown below:</p>
<pre class="highlight"><;bean class="com.dineshonjava.sdnext.beanscope.Point" id="zeroPoint" scope="prototype">;<;property name="x" value="0">;<;/property>; 
 <;property name="y" value="0">;<;/property>; 
<;/bean>; 
</pre>
<p><b><a href="https://dineshonjava.com/prototype-bean-scope-with-annotation/">See the full example of prototype bean scope with using the Annotation.</a></b></p>
<div style="color: #274e13;"><b>Using Annotation for Bean Scope:</b></div>
<pre class="highlight">@Service 
@Scope("prototype") 
public class Point 
{ 
 private int x; 
 private int y; 
 
 public void setX(int x){ 
 this.x = x; 
 } 
 
 public void setY(int y){ 
 this.y = y; 
 } 
} 
</pre>
<p><b><a href="https://dineshonjava.com/prototype-bean-scope-with-annotation/">See the full example of prototype bean scope with using the Annotation.</a></b></p>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/06/prototype.jpg" width="640" height="323" border="0" /></div>
<p> ;</p>
</div>
<div style="color: blue;"><b>There are three wen application aware bean scope are given below.</b></div>
<h2 style="color: #274e13;"><b>1. Request Scope:</b></h2>
<div style="color: #274e13;">This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.</div>
<pre class="highlight" style="color: #274e13;"><;bean class="com.dineshonjava.Point" id="point" scope="request">;<;/bean>; 
</pre>
<h2 style="color: #274e13;"><b>2. Session Scope:</b></h2>
<div style="color: #274e13;">This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.</div>
<pre class="highlight" style="color: #274e13;"><;bean class="com.dineshonjava.Point" id="point" scope="session">;<;/bean>; 
</pre>
<h2 style="color: #274e13;"><b>3. global-session:</b></h2>
<div style="color: #274e13;">This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.</div>
<pre class="highlight" style="color: #274e13;"><;bean class="com.dineshonjava.Point" id="point" scope="globalSession">;<;/bean>; 
</pre>
<div style="color: #274e13;">The global session scope is similar to the standard HTTP Session scope (described immediately above), and really only 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 (or bound) to the lifetime of the global portlet Session.</div>
<div style="color: #274e13;">
<p>Please note that 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.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Spring Related Topics you may like</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/interview-questions-on-spring/"><span style="color: red;">Spring Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/"><span style="color: red;">Spring AOP Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/"><span style="color: red;">Spring MVC Interview Questions</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-interview-questions-and-answers/"><span style="color: red;">Spring Security Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-rest-web-services-interview-questions-and-answers/"><span style="color: red;">Spring REST Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-interview-questions-and-answers/"><span style="color: red;">Spring Boot Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-microservices-interview-questions-answers/"><span style="color: red;">Spring Boot Microservices Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/dependency-injection-in-spring/"><span style="color: red;">Dependency Injection (DI) in Spring </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-ioc-container/"><span style="color: red;">Spring IoC Container</span></a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/"><span style="color: red;">What is Bean Factory in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/application-context-in-spring/"><span style="color: red;">ApplicationContext in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-autowiring-in-spring/"><span style="color: red;">Bean Autowiring in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-bean-scopes/"><span style="color: red;">Spring Bean Scopes</span></a></b></li>
<li><b><a href="https://dineshonjava.com/create-custom-bean-scope-in-spring-example/"><span style="color: red;">Create Custom Bean Scope in Spring Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/using-applicationcontextaware-in-spring/"><span style="color: red;">Using ApplicationContextAware in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-lifecycle-and-callbacks/"><span style="color: red;">Spring Bean Life Cycle and Callbacks</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-beanpostprocessor-in-spring/"><span style="color: red;">BeanPostProcessor in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-beanfactorypostprocessor-in/"><span style="color: red;">BeanFactoryPostProcessor in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/annotations-in-spring-and-based/"><span style="color: red;"> Annotations in Spring and Based Configuration</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-jsr-250-annotations/"><span style="color: red;">Spring JSR-250 Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/jsr-330-annotations-in-spring/"><span style="color: red;">JSR 330 Annotations in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/"><span style="color: red;">Spring @Component, @Repository, @Service and @Controller Stereotype Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/method-injection-with-spring-using-lookup-method-property/"><span style="color: red;">Method injection with Spring using Lookup method property</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-aop-in-spring/"><span style="color: red;">Spring AOP-Introduction to Aspect Oriented Programming</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aspect-annotation/"><span style="color: red;">@Aspect Annotation in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-before-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Before Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @After Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-returning-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-returning-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After-Returning Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-throwing-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Throwing Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-around-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Around Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-around-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Around Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-aop-proxies-chapter-31/"><span style="color: red;">Spring AOP Proxies in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-transaction-management-in/"><span style="color: red;"> Spring AOP Transaction Management in Hibernate</span></a></b></li>
<li><b><a href="https://dineshonjava.com/transaction-management-in-spring/"><span style="color: red;">Spring Transaction Management</span></a></b></li>
<li><b><a href="https://dineshonjava.com/declarative-transaction-management/"><span style="color: red;">Spring Declarative Transaction Management Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-ordering-of-aspects-with-example/"><span style="color: red;">Spring AOP-Ordering of Aspects with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-java-based-configuration-with-example/"><span style="color: red;">Spring Security Java Based Configuration with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-xml-namespace-configuration-example/"><span style="color: red;">Spring Security XML Namespace Configuration Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
</div>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-autowiring-by-type/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-autowiring-by-constructor/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '670'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…