<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;"><b>Prototype Bean Scope: </b>If scope is set to prototype, the <a href="https://dineshonjava.com/2012/06/spring-ioc-container.html" style="color: #274e13;">Spring IoC container</a> creates new bean instance of the object every time a request for that specific bean is made. <b style="color: #274e13;">As a rule, use the prototype scope for all state-full beans and the singleton scope for stateless beans.</b></p>
<div align="center" id="ads-id"></div>
<p><b style="color: #274e13;"><br />
</b> </p>
<div style="color: #274e13;"><b><i>Lets see with example which show the prototype scope of the &#8216;zeroPoint&#8217; bean in the spring container.</i></b><br />
<b><i><br />
</i></b></div>
</div>
<p><b>Point.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.beanscope.tutorial;

public class Point
{
 private int x;
 private int y;
 /**
 * @return the x
 */
 public int getX() {
 return x;
 }
 /**
 * @param x the x to set
 */
 public void setX(int x) {
 this.x = x;
 }
 /**
 * @return the y
 */
 public int getY() {
 return y;
 }
 /**
 * @param y the y to set
 */
 public void setY(int y) {
 this.y = y;
 }
}
</pre>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;"><b>Popular Tutorials</b></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html"><em><strong>Spring Boot Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint.html"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/spring-hateoas-hypermedia-driven-restful-web-service.html"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/microservices-with-spring-boot.html"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/03/hibernate-3-on-baby-steps.html"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-batch-process-with-example.html"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b><br />
</b> <b>spring.xml</b></p>
<pre class="highlight" name="code"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:><b><br />
</b> <b>DrawingApp.java</b><br />
<pre class="highlight" name="code">package com.dineshonjava.sdnext.beanscope.tutorial;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Dinesh Rajput
 *
 */
public class DrawingApp 
{
 /**
 * @param args
 */
 public static void main(String[] args) 
 {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

Point point1 = (Point) context.getBean("zeroPoint");
Point point2 = (Point) context.getBean("zeroPoint");
if(point1 == point2)
 System.out.println("In Singleton Demo >;>; Both are same objects values are.." +
 "n 1. point1= "+point1+" n 2. point2= "+point2);
else
 System.out.println("In Protptype Demo >;>; Both are different objects values are.." +
 "n 1. point1= "+point1+" n 2. point2= "+point2);
 }
}
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<div style="color: red;">Jun 28, 2012 9:10:46 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh</div>
<div style="color: red;">INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ab50cd: startup date [Thu Jun 28 21:10:46 IST 2012]; root of context hierarchy</div>
<div style="color: red;">Jun 28, 2012 9:10:46 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions</div>
<div style="color: red;">INFO: Loading XML bean definitions from class path resource [spring.xml]</div>
<div style="color: red;">Jun 28, 2012 9:10:46 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons</div>
<div style="color: red;">INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1543c88: defining beans [zeroPoint]; root of factory hierarchy</div>
<p><b>In Protptype Demo >;>; Both are different objects values are..<br />
 ;1. point1= com.dineshonjava.sdnext.beanscope.tutorial.Point@d3d6f <br />
 ;2. point2= com.dineshonjava.sdnext.beanscope.tutorial.Point@13c468a</b></div>
<div style="color: #274e13;">
Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the above message:<b> See the both address value of the object is different.</b></div>
<p></p>
<div style="color: blue;"><b>Bean scopes using annotation:</b></div>
<p><b><br />
</b> <b>spring.xml</b></p>
<pre class="highlight" name="code"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:><div style="color: #274e13;"><br />
Here <b>component-scan</b> element has attribute '<b>base-package</b>'. base-package contain the base package of the Bean class.</div><div style="color: #274e13;"><b><br />
</b> <b>base-package = "com.dineshonjava.sdnext.beanscope.tutorial"</b></div><b><br />
</b> <b>Ponit.java</b><br />
<pre class="highlight" name="code">package com.dineshonjava.sdnext.beanscope.tutorial;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope("prototype")
public class Point
{
 private int x;
 private int y;
 /**
 * @return the x
 */
 public int getX() {
 return x;
 }
 /**
 * @param x the x to set
 */
 public void setX(int x) {
 this.x = x;
 }
 /**
 * @return the y
 */
 public int getY() {
 return y;
 }
 /**
 * @param y the y to set
 */
 public void setY(int y) {
 this.y = y;
 }
}
</pre>
<div style="color: blue;"><b><br />
</b> <b>Annotation @Service:</b></div>
<div style="color: #274e13;"><b>Target </b>: Class</div>
<div style="color: #274e13;"><b>Description</b>:</div>
<div style="color: #274e13;">Here <b>@Service</b> is streotype annotation which declared above the class name that means this class is a bean of the spring container.</div>
<div style="color: #274e13;">In <b>Spring container</b> this bean recognized by the id "<b>point</b>" means that <b>same as the class name except first letter of the class name is the small letter</b>.</div>
<div style="color: #274e13;">for example: class '<b>Point</b>' get as '<b>point</b>' bean</div>
<div style="color: #274e13;"> ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; class '<b>Circle</b>' get as '<b>circle</b>' bean</p>
<div style="color: blue;"><b><br />
</b> <b>Annotation @Scope:</b></div>
<div style="color: #274e13;"><b>Target </b>: Class</div>
<div style="color: #274e13;"><b>Description</b>:</div>
<div style="color: #274e13;">Here <b>@Scope </b>is annotation which declared above the class name that define the scope the bean</div>
<p> ;<b>@Scope("singleton")</b><br />
 ;<b>@Scope("prototype")</b><b> </b><br />
<b><br />
</b></div>
<p><b>DrawingApp.java</b></p>
<pre class="highlight" name="code">import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dineshonjava.sdnext.beanscope.tutorial.Point;
/**
 * @author Dinesh Rajput
 *
 */
public class DrawingApp 
{
 /**
 * @param args
 */
 public static void main(String[] args) 
 {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Point point1 = (Point) context.getBean("point");
Point point2 = (Point) context.getBean("point");
if(point1 == point2)
 System.out.println("In Singleton Demo >;>; Both are same objects values are.." +
 "n 1. point1= "+point1+" n 2. point2= "+point2);
else
 System.out.println("In Protptype Demo >;>; Both are different objects values are.." +
 "n 1. point1= "+point1+" n 2. point2= "+point2);
 }
}
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<p>Jun 28, 2012 10:59:39 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh<br />
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@758fc9: startup date [Thu Jun 28 22:59:39 IST 2012]; root of context hierarchy<br />
Jun 28, 2012 10:59:39 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions<br />
INFO: Loading XML bean definitions from class path resource [spring.xml]<br />
Jun 28, 2012 10:59:39 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons<br />
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bb7759: defining beans [point,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy<b><br />
In Protptype Demo >;>; Both are different objects values are..<br />
 ;1. point1= com.dineshonjava.sdnext.beanscope.tutorial.Point@d3d6f <br />
 ;2. point2= com.dineshonjava.sdnext.beanscope.tutorial.Point@13c468a</b></div>
</div>
<p></p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><b>Spring Related Topics you may like</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/10/interview-questions-on-spring.html">Spring Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-interview-questions-and-answers.html">Spring AOP Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html">Spring MVC Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-security-interview-questions-and-answers.html">Spring Security Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-rest-web-services-interview-questions-and-answers.html">Spring REST Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/04/spring-boot-interview-questions-and-answers.html">Spring Boot Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/04/spring-boot-microservices-interview-questions-answers.html">Spring Boot Microservices Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/dependency-injection-in-spring.html">Dependency Injection (DI) in Spring </a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/spring-ioc-container.html">Spring IoC Container</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/what-is-bean-factory-in-spring.html">What is Bean Factory in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/application-context-in-spring.html">ApplicationContext in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/bean-autowiring-in-spring.html">Bean Autowiring in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/understanding-bean-scopes.html">Spring Bean Scopes</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/create-custom-bean-scope-in-spring-example.html">Create Custom Bean Scope in Spring Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/using-applicationcontextaware-in-spring.html">Using ApplicationContextAware in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/bean-lifecycle-and-callbacks.html">Spring Bean Life Cycle and Callbacks</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/writing-beanpostprocessor-in-spring.html">BeanPostProcessor in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/writing-beanfactorypostprocessor-in.html">BeanFactoryPostProcessor in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/annotations-in-spring-and-based.html"> Annotations in Spring and Based Configuration</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/spring-jsr-250-annotations.html">Spring JSR-250 Annotations</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/jsr-330-annotations-in-spring.html">JSR 330 Annotations in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/stereotype-annotations-in-spring.html">Spring @Component, @Repository, @Service and @Controller Stereotype Annotations</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/method-injection-with-spring-using-lookup-method-property.html">Method injection with Spring using Lookup method property</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/introduction-to-aop-in-spring.html">Spring AOP-Introduction to Aspect Oriented Programming</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/spring-aspect-annotation.html">@Aspect Annotation in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-before-annotation-advice-example.html"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-before-advice-example-using-xml-config.html"> Spring AOP Before Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-after-annotation-advice-example.html"> Spring AOP AspectJ @After Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-after-advice-example-using-xml-config.html"> Spring AOP After Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-after-returning-annotation-advice-example.html"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-after-returning-advice-example-using-xml-config.html"> Spring AOP After-Returning Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-after-throwing-annotation-advice-example.html"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-after-throwing-advice-example-using-xml-config.html"> Spring AOP After Throwing Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-around-annotation-advice-example.html"> Spring AOP AspectJ @Around Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-around-advice-example-using-xml-config.html"> Spring AOP Around Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/understanding-aop-proxies-chapter-31.html">Spring AOP Proxies in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-aop-transaction-management-in.html"> Spring AOP Transaction Management in Hibernate</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/transaction-management-in-spring.html">Spring Transaction Management</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/declarative-transaction-management.html">Spring Declarative Transaction Management Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-ordering-of-aspects-with-example.html">Spring AOP-Ordering of Aspects with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-security-java-based-configuration-with-example.html">Spring Security Java Based Configuration with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-security-xml-namespace-configuration-example.html">Spring Security XML Namespace Configuration Example</a></b></li>
</ol>
</div>
<div style="color: #274e13;"><b> ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; <a href="https://dineshonjava.com/2012/06/understanding-bean-scopes.html">Bean Scope in The Spring</a></b></div>
</div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/singleton-bean-scope-in-spring/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/using-applicationcontextaware-in-spring/">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: '666'});
});
</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…