<p>In this article, we will discuss the complex scenario of Spring bean scopes. As we know the default bean scope of the bean is a singleton. Whenever we call <em>getbean </em>every time we will get the same bean instance from the application context. When application initialized JVM read all the configuration from the XML file or from the config class and loaded beans in the application context with a default bean scope singleton.</p>
<h2>Spring Bean Scopes</h2>
<p>To understand what happened in the background we need to enable the debug mode by adding below code in the <em>application.properties</em> file.</p>
<pre class="highlight">logging.level.org.springframework=debug 
</pre>
<p>Show that we can identify what is happing in the background when we initialized the application.</p>
<p>Now in this article, we will discuss only prototype and singleton and its different way of use in the application.</p>
<table>
<tbody>
<tr>
<td colspan="2" width="378">Bean Scope</td>
</tr>
<tr>
<td width="124">Singleton</td>
<td width="254">One instance per spring context</td>
</tr>
<tr>
<td width="124">Prototype</td>
<td width="254">New bean whenever requested</td>
</tr>
</tbody>
</table>
<p>Let&#8217;s create a java class to understand the above-mentioned scope.<br />
<strong>RepoDaoLayer.java</strong></p>
<pre class="highlight">@Component 
public class RepoDaoLayer { 
 
	 @Autowired 
	 DatabaseConnection jdbcConnection; 
 
	public DatabaseConnection getJdbcConnection() { 
		return jdbcConnection; 
	} 
 
	public void setJdbcConnection(DatabaseConnection jdbcConnection) { 
		this.jdbcConnection = jdbcConnection; 
	} 
} 
</pre>
<p>As we can see in the above-mentioned code <em>RepoDaoLayer</em> has a dependency of <em>DatabaseConnection </em>bean. Whenever we initialized our application, spring will create an instance of <em>DatabaseConnection</em> along with an instance of <em>RepoDaolayer</em>.</p>
<p>Now let&#8217;s Create <em>DatabaseConnection.java</em> file</p>
<pre class="highlight">@Component 
public class DatabaseConnection { 
 
	 
	 public DatabaseConnection() { 
		 System.out.println("JDBC COONECITON"); 
	 } 
} 
</pre>
<p>Now Create <em>DemoApplication.java</em> main file to run the code.</p>
<pre class="highlight">@SpringBootApplication 
public class DemoApplication { 
static Logger LOG= LoggerFactory.getLogger(DemoApplication.class); 
	public static void main(String[] args) { 
			 
ApplicationContextcxt=SpringApplication.run(DemoApplication.class, args);	 
RepoDaoLayer dao= cxt.getBean(RepoDaoLayer.class); 
	 
LOG.info(dao.toString()); 
LOG.info(dao.getJdbcConnection().toString()); 
	 
RepoDaoLayer da1= cxt.getBean(RepoDaoLayer.class); 
LOG.info(da1.toString()); 
LOG.info(da1.getJdbcConnection().toString()); 
		 
	} 
} 
 
</pre>
<p>As we can see we are getting two instances of <em>RepoDaoLayer</em> class using <strong><em>getBean</em></strong>. When we run the above code we will get below output:</p>
<p><img class="aligncenter wp-image-4191 size-full" src="https://dineshonjava.com/wp-content/uploads/2018/07/output.png" alt="spring bean scopes" width="547" height="141" /></p>
<p>Here we are getting the same instance of <em>RepoDaolayer</em> &; <em>DatabaseConnection</em> every time because the default bean scope is the singleton.</p>
<p><em><strong>Now What will happen when the parent class is prototype and child is a singleton?</strong></em></p>
<p>Now I am making <em>RepoDaoLayer.java</em> class prototype. By using <em>@scope(“prototype”)</em> over <em>RepoDaolayer.java</em> class</p>
<pre class="highlight">@Component 
@scope(“prototype”) 
public class RepoDaoLayer { 
 
... 
} 
</pre>
<p>Now run the main java file you will get below output:</p>
<p><img class="size-full wp-image-4192 aligncenter" src="https://dineshonjava.com/wp-content/uploads/2018/07/output-2.png" alt="output-2" width="411" height="142" /></p>
<p>As we can see in the screenshot we are getting the different instance of <em>RepoDaolayer</em> but same <em>DatabaseConnection</em> instance every time because of singleton nature of <em>DatabaseConnection</em>.</p>
<p><em><strong>Now, what with happen if you make parent class as a singleton and child class as a prototype?</strong></em></p>
<p>I am using <em>@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) </em>in <em>DatabaseConnection.java</em> class.</p>
<pre class="highlight">@Component 
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 
public class DatabaseConnection { 
... 
} 
</pre>
<p>Now run the main class. You will get the below output.</p>
<p><img class="aligncenter wp-image-4193 size-full" src="https://dineshonjava.com/wp-content/uploads/2018/07/output-3.png" alt="output-3" width="410" height="107" /><br />
<em><strong>Now we can see we are getting same bean instances every time even we make DatabaseConnection class as a prototype? Why are we getting the same bean?</strong></em></p>
<p>Because <em>RepoDaoLayer</em> class is singleton by nature and when we ask spring to give me bean of <em>RepoDaolayer</em> class it gives us a singleton bean without realizing that <em>databaseConnection</em> bean is the prototype.</p>
<p>If we really want to get different beans of <em>DatabaseConnection</em> whenever we request to <em>Repodaolayer</em> then we need to configure Proxy. The proxy will make sure that spring will give us new instances every time when we call <em>RepoDaolayer</em> bean. Below is the code to configure proxy bean of <em>DatabaseConnecion.java</em> class so that we can get new bean every time when we call Singleton bean.</p>
<pre class="highlight">@Component 
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE,proxyMode=ScopedProxyMode.TARGET_CLASS) 
public class DatabaseConnection { 
... 
} 
</pre>
<p>Here we use something called <em>proxyMode=ScopedProxyMode.TARGET_CLASS</em>. Which make sure that each time we get new bean when we call <em>RepoDaoLayer</em>. Now we can get different bean of <em>DatabaseConnection</em> every time when you get an instance of <em>RepoDaolayer</em>.</p>
<p><img class="size-full wp-image-4194 aligncenter" src="https://dineshonjava.com/wp-content/uploads/2018/07/output-4.png" alt="output-4" width="480" height="205" /></p>
<p>Here we can see we are getting same instance of <em>RepoDaolayer</em> but different instance of <em>DatabaseConnection</em> class because of proxy. Instead of giving <em>DatabaseConnection</em> object spring give us the proxy object of <em>DatabaseConnection</em>.</p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/mastering-spring-boot-2-0-and-spring-cloud/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/designing-applications-with-spring-boot-and-react-js/">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: '4189'});
});
</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…