Spring takes you through the Bean life cycle (singleton and prototype)

Posted by jordy on Thu, 16 Dec 2021 09:29:30 +0100

Bean life cycle (singleton and prototype)

About Bean lifecycle:
Spring chooses the management mode according to the scope of the Bean. For a singleton scoped Bean, spring can accurately know when the Bean is created, when initialization is completed, and when it is destroyed; Spring is only responsible for creating beans with prototype scope. When the container creates an instance of a Bean, the instance of the Bean is handed over to the client code management, and the spring container will no longer track its life cycle.

How to verify it?

First, you need to read a basic knowledge, which takes about 5 minutes, and then we will do a test. The answer will naturally enable you to understand the life cycle of Bean.

Link: Spring Bean lifecycle.

First, we test the singleton pattern

The code is as follows

<?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="helloWorld" class="net.biancheng.HelloWorld" scope="singleton"
    	init-method="init" destroy-method="destroy">
        <property name="message" value="Hello World!" />
    </bean>

</beans>
package net.biancheng;

public class HelloWorld {
    private String message;

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

    public void getMessage() {
        System.out.println("message : " + message);
    }
    
    private void init() {
		System.out.println("test xml Configured initialization method");
	}
    
    private void destroy() {
    	System.out.println("test xml Configured destruction method");
	}
}
package net.biancheng;

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

public class MainApp {
    public static void main(String[] args) {
    	//Spring instance
//        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    	AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.setMessage("object A");//Note that 1-getBean objects twice
        obj.getMessage();
        context.registerShutdownHook();//Note 2 - destroy is invoked only once
        
        //Test singleton mode and prototype mode
        HelloWorld obj2 = (HelloWorld) context.getBean("helloWorld");
        obj2.getMessage();
    }
}
test xml Configured initialization method
message : object A
message : object A
 test xml Configured destruction method

Therefore, it is verified that for a singleton scoped Bean, Spring can accurately know when the Bean is created, when initialization is completed, and when it is destroyed;

Spring knows all the life cycles of the Bean, so it calls the initialization and destruction methods. Because it verifies that there is only one object in the container, it only calls the initialization method once, and the destruction method is called last.

Finally, we test the prototype pattern prototype

We only change the xml configuration

<?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="helloWorld" class="net.biancheng.HelloWorld" scope="prototype"
    	init-method="init" destroy-method="destroy">
        <property name="message" value="Hello World!" />
    </bean>

</beans>

var foo = 'bar';
test xml Configured initialization method
message : object A
 test xml Configured initialization method
message : Hello World!

Therefore, it is verified that Spring is only responsible for creating beans in the prototype scope. When the container creates an instance of the Bean, the instance of the Bean will be handed over to the client code management, and the Spring container will no longer track its life cycle.

Spring is only responsible for creating, so it calls the initialization method instead of the destruction method. At the same time, it verifies the prototype pattern. Each time the spring container obtains a Bean, the container will create a Bean instance, so it will call the initialization method twice.

Topics: Java Spring