The life cycle of spring beans

Time:2020-2-12

2.1 life cycle process

1. Build (constructor: or construct)

2. Initialization (init)

3. Destroy

A more complex life cycle is:

1, build

2. Set properties

3. Preprocessing method of postprocessor: beanpostprocessor.postprocessbeforeinitialization

4. Initialization (init)

5. Postprocessing method of postprocessor: beanpostprocessor.postprocessafterinitialization

6, destroy

Each step has its own work. For example, if we want to finish some work before the bean is injected into IOC, or we want to modify the properties of the setter. You need to go to the corresponding step to modify the corresponding processor or method.

We will talk about the specific corresponding steps later.

Build features:

If it is a single instance: create the object when the container starts. Injection to IOC

If it is multi instance: create the object every time you get it

Initialization:

After the object is created and assigned, the initialization method is called.

Destruction:

Single instance: when the container is closed

Multi instance: the container will not close the bean, and the container will not call the destroy method.

2.2 custom initialization and destruction methods

1. Injection bean

@Bean(initMethod = "initMethod", destroyMethod = "destoryMethod")
public Car car() {
  return new Car();
}

The custom init and destroy methods are implemented by developing init method and destroy method.

2. Through interface

public class Cat implements InitializingBean, DisposableBean {

    public Cat() {
        System.out.println("Cat...constructor");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System. Out. Println ("init method after cat... Attribute assignment");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("Cat...destroy");
    }

}

Implement the interface of initializingbean, and complete the work before init through the method afterpropertieset. (see also after the property is set). Through the destroy method to complete the destroy method.

3. Through jsr250

@PostConstruct
public void initMethod() {
  System.out.println("Dog...init--- PostConstruct");
}

@PreDestroy
public void destroyMethod() {
  System.out.println("Dog...destroy--PreDestroy");
}

Use @ postconstruct and @ predestroy annotations.

4、BeanPostProcessor

The postprocessor is spring’s coolest feature.

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        System.out.println("Bean Post Processor....postProcessBeforeInitialization...." + beanName + bean);

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Bean Post Processor....postProcessAfterInitialization" + beanName+ bean);

        return bean;
    }
    
}

Spring’s underlying annotations, such as bean assignment and injection of other components, @ async is implemented through beanpostprocessor

2.3 debug process

Key steps:

The life cycle of spring beans

Populatebean (beanname, MBD, instancewrapper): complete the assignment of attributes

Initializebean (beanname, exposedobject, MBD); completes the initialization.

Protect the following steps:
The life cycle of spring beans

applyBeanPostProcessorsBeforeInitialization:

invokeInitMethods(beanName, wrappedBean, mbd);

applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

The life cycle of spring beans