Spring5 IOC container bean management scope and lifecycle of beans

Scope of bean

1. In Spring, you can set whether the created bean instance is a single instance or a multi instance (single instance by default)

Single instance: only one object

Multi instance: create a new object each time

2. In Spring, by default, bean instances are single instance objects

@Test
public void testCollection2(){
  ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
  Book book1 = context.getBean("book", Book.class);

  Book book2 = context.getBean("book",Book.class);
  System.out.println(book1);
  System.out.println(book2);
  //The output addresses are the same, so it is a single instance object by default
}

3. How to set single instance or multi instance

(1) In the Bean tag of the Spring configuration file, the attribute scope is used to set single instance or multi instance

There are multiple scope values and two common values

singleton represents a single instance object

prototype represents a multi instance object

 

If you execute the above code again, you will find that the two addresses are different

 

(2) singleton is different from prototype

First, single indicates single instance, and prototype indicates multiple instances

Second: when the scope value is set to singleton, a single instance object will be created when the spring configuration file is loaded (such as hungry man)

When the scope value is set to prototype, the object is not created when the spring configuration file is loaded, but the multi instance object is created when the getBean() method is called (such as lazy)

Besides singleton and prototype, there are also Request and Session

 

2. bean lifecycle

Life cycle: the process from object creation to object destruction

 

The lifecycle of the bean is as follows:

(1) Create bean instance through constructor (parameterless constructor)

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

(3) Call the initialization method of the bean (configuration required)

(4) The bean is ready for use (the object is obtained)

(5) When the container is closed, the bean destruction method is called (the destruction method needs to be configured)

 

Demonstrate the lifecycle of a bean

Create Orders class

public class Orders {
    public Orders(){
        System.out.println("The first step is to execute the construction without parameters to create bean example");
    }

    private String oname;

    public void setOname(String oname) {
        this.oname = oname;
        System.out.println("Step 2 call set Method to set a property value");
    }
    //Create an initialization method for execution
    public void initMethod(){
        System.out.println("Step 3 execute initialization method");
    }

    //Create a method for performing destruction
    public void destroyMethod(){
        System.out.println("Step 5 execute the destruction method");
    }
}

 

The configuration file is as follows:

<bean id="Orders" class="bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
  <property name="oname" value="phone"></property>
</bean>

 

Test method:

@Test
public void testBean4(){
  //ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
  // Because in ApplicationContext None in close()Method, so its implementation class needs to be used ClassPathXmlApplicationContext
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");   Orders orders = context.getBean("Orders", Orders.class);   System.out.println("Step 4 get and create bean Object of the instance");   //Destruction needs to be done manually   context.close();//Here we call Order Medium destroy method }

 

 

After adding the post processor of the bean, the life cycle of the bean is as follows (a total of seven steps):

(1) Create bean instance through constructor (parameterless constructor)

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

(3) Before initialization, pass the bean instance to the bean's postprocessbeforeinitialization method (object bean, string beanname)

(4) Call the initialization method of the bean (configuration required)

(5) After initialization, pass the bean instance to another method of the bean's post processor, postProcessAfterInitialization(Object bean, String beanName)

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

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

 

Show me the effect of adding a post processor

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

Create a class MyBeanPost as the post processor

public class MyBeanPost implements BeanPostProcessor {
    //These two methods are default Method, not implemented, here for demonstration
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Methods executed before initialization postProcessBeforeInitialization");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Methods executed after initialization postProcessAfterInitialization");
        return bean;
    }
}

 

Configure the post processor in the configuration file (after the post processor is configured, the post processor will be added to all bean s in the configuration file)

<!--Configure the post processor, which will make the configuration file bean Add post processor processing-->
<bean id="myBeanPost" class="bean.MyBeanPost"></bean>

 

 

Posted by leeandrew on Mon, 30 May 2022 17:51:51 +0530