The life cycle of spring beans


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


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


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() {

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

    public void destroy() throws Exception {


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

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

public void destroyMethod() {

Use @ postconstruct and @ predestroy annotations.


The postprocessor is spring’s coolest feature.

public class MyBeanPostProcessor implements BeanPostProcessor {

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

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

        return bean;

    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


invokeInitMethods(beanName, wrappedBean, mbd);

applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

The life cycle of spring beans