Spring Bean Prototype

By | | Updated : 2021-03-09 | Viewed : 2961 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.


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 Bean Prototype Example

Please update pom.xml with below xml file.

<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">

Create a SpringBean class a given below.

* @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.

<?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

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


Please create the TestMainClient class as given bellow.

* @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 Bean Prototype Example Result

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

Leave A Reply