Spring Bean Scopes - Prototype

By | | Updated : 2018-01-01 | Viewed : 644 times

Spring Bean Scopes Prototype

Next type of spring bean scope is Prototype. The prototype typed Spring bean has been created whenever we request Spring Container for object creation/instantiation by getBean () method call or injected dependency bean into dependent bean.

If we mention scope as prototype in springbeans-config.xml, spring bean is instantiated for each specific bean request to Spring container.

Prototype

Note: The spring container does not manage a callback for destruction of prototype of object. Means prototype object is not destroyed by Spring container. Developer need to write code for prototype destruction. Here we will learn for customization of bean-post-processor to destroy the Prototype typed spring bean

Please create the below given Maven project.

Spring Scopes Prototype

Please update pom.xml with below xml file.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.docsconsole.spring</groupId>
  <artifactId>SpringBeanScopes-Prototype</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
	 <dependency>
	 <groupId>org.springframework</groupId>
	 <artifactId>spring-context</artifactId>
	 <version>3.0.0.RELEASE</version>
	 </dependency>
  </dependencies>
  
</project>

Create a SpringBean class a given below.

SpringBean.java
/**
* 
* @author:Amar
* @since:2017-05-19 
*
*/

package com.docsconsole.spring;

public class SpringBean {

	public String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
}

Please create the springbeans-config.xml as given bellow.

springbeans-config.xml
<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	   <bean id = "springBean" class = "com.docsconsole.spring.SpringBean" scope = "singleton">
	   </bean>

</beans>

Please create the TestMainClient class as given bellow.

TestMainClient.java
/**
* 
* @author:Amar
* @since:2017-05-19 
*
*/

package com.docsconsole.spring;

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

public class TestMainClient {
	
	public static void main(String[] args) {
		
		System.out.println("------------Main mehtod has been started here------------------------------");
		ApplicationContext context = new ClassPathXmlApplicationContext("com/docsconsole/spring/springbeans-config.xml");
		System.out.println("------------ApplicationContext has been created her------------------------");
		
		SpringBean aBean = (SpringBean) context.getBean("springBean");
		System.out.println("------------aBean SpringBean object has been created------------------------");
		System.out.println("------------SpringBean object hashcode------------------------"+aBean.hashCode());
		

		SpringBean bBean = (SpringBean) context.getBean("springBean");
		System.out.println("------------bBean SpringBean object has been created------------------------");
		System.out.println("------------SpringBean object hashcode------------------------"+bBean.hashCode());
	}
	
}

If you observe the output, out will be as given bellow.

Spring Scopes Prototype Result

Here two objects is created as per two requests for creation.

Leave A Reply