Scope and lifecycle of Bean

Keywords: Java Spring ioc bean

1. Scope of bean

In Spring, set whether to create a bean instance as a single instance or multiple instances. Generally speaking, our configuration file is in xml

<bean id="name" class="route">

After this configuration, we can get the bean instance we created through our test class. In Spring. The bean instances we create can be single instance objects or multiple instance objects. By default, it is a singleton object, which can be set in Spring. When we create a bean instance, whether it is a singleton or a multi instance is called the scope of the bean. A single instance has only one object, and multiple instances will create a new object each time.

Here I have an example to verify

//Book class
public class Book {
    private String bname;
    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
}



//xml configuration file

    <bean id="book" class="com.company.Book">
        <property name="bname" value="zwz1"></property>
        <property name="bauthor" value="zwz2"></property>
    </bean>



//Test class

public class Spring5Test {
    @Test
    public  void testBook1(){

        //Load spring configuration file
        ApplicationContext context =new ClassPathXmlApplicationContext("bean1.xml");

        //Gets the object created by the configuration
        Book book1=context.getBean("book",Book.class);
        Book book2=context.getBean("book",Book.class);
        System.out.println(book1);
        System.out.println(book2);
    }

}

If you want to verify whether the number of Bean instances I create is a singleton object or a multi instance object, if the addresses of the two objects I output, book1 and book2, it means that I create a singleton object. give the result as follows

Obviously, the beans I create are singleton scopes by default.

How to set single instance or multi instance  

(1) In the bean tag of spring configuration file, there is a property (scope) to set single instance or multiple instances

(2) The first value of the scope attribute value is the default value, singleton, which indicates that it is a single instance object, and the second value, prototype, indicates that it is a multi instance object

We modify the configuration file

 <bean id="book" class="com.company.Book" scope="prototype">
        <property name="bname" value="zwz1"></property>
        <property name="bauthor" value="zwz2"></property>
    </bean>

Everything else remains the same, and the result is obvious

The output address values are different, indicating that it is not an object, but multi instance.

  The difference between singleton and prototype

1. singleton single instance, prototype multi instance

2. When the scope value is set to singleton, a single instance object will be created when the spring configuration file is loaded. When the scope value is set to prototype, the object will not be created when the spring configuration file is loaded. When the getBean method is called, a multi instance object will be created

Bean lifecycle

1. What is the life cycle of a Bean

Bean life cycle, that is, the process from generation to destruction of beans.

bean lifecycle process

(1) Create bean instance through constructor (no parameter construction)

(2) Set values for bean properties and references to other beans (call the set method)

(3) Call the initialization method of the bean (the initialization method needs to be configured in the xml file)

(4) Gets the object of the Bean

(5) When the container is closed, call the bean destruction method (the destruction method needs to be configured in the xml file)

Let's demonstrate this process

 

 

 

package com.company;

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

public class Spring5Test {
    @Test
    public  void testBook1(){

        //Load spring configuration file
        ApplicationContext context =new ClassPathXmlApplicationContext("bean1.xml");

        //Gets the object created by the configuration
        Orders orders=context.getBean("orders",Orders.class);

        System.out.println("Step 4 get and create bean Instance object");
        System.out.println(orders);
     
    }

}

Final output result:

 

  There are 7 post-processing steps for beans. There will be one step before and after initialization, as follows:

(1) Create bean instance through constructor (no parameter construction)

(2) Set values for bean properties and references to other beans (call the set method)

(3) Pass the bean instance to the bean post processor's method postProcessBeforeInitialization

(4) Call the initialization method of the bean (the method that needs configuration initialization)

(5) Pass the bean instance to the bean post processor method postProcessAfterInitialization

(6) The bean is ready to use (the object is obtained)

(7) When the container is closed, call the bean destruction method (the method that needs to be configured for destruction)

  Demonstrate the effect of adding a post processor

1. Create a class, implement the interface BeanPostProcessor, and create a post processor

public class MyBeanPost implements BeanPostProcessor {
 @Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) 
throws BeansException {
 System.out.println("Method executed before initialization");
 return bean;
 }
 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) 
throws BeansException {
 System.out.println("Method executed after initialization");
 return bean;
 }
}

2. Register xml

  3. Results

  There is a problem that we just created a MyBeanPost bean, and the object created in the test class is Orders. We did not directly create the MyBeanPost object, but why do the methods we wrote in the MyBeanPost class still execute?

The reason is that when we load the configuration file, we will create the Orders object and MyBeanPos object, and MyBeanPos implements the BeanPostProcessor interface. Spring will execute this class as a post processor, and the post processor will add the post processor processing to all the current configuration files. That is, it is processed before and after initialization. If there are multiple beans in it, the post processor will be added to multiple beans.

Posted by Tsukasa on Thu, 07 Oct 2021 11:02:40 -0700