细品Spring Bean的生命周期

spring bean的生命周期官方的流程图如下:

接下来 我们用代码验证一下是否如图所愿:

首先定义一个testBean

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 *
 * @author runtu
 * @version TestBean, v2.0 2019/12/13 9:19
 **/
public class TestBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
                      InitializingBean, DisposableBean {

    public TestBean() {
        System.out.println("=====================构造方法========================");
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("=====================BeanNameAware========================");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("=====================BeanFactoryAware========================");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("=====================ApplicationContextAware========================");
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println("=====================InitializingBean========================");
    }

    @Override
    public void destroy() {
        System.out.println("=====================DisposableBean========================");
    }

    public void init() {
        System.out.println("=====================init========================");
    }

    public void shutdown() {
        System.out.println("=====================shutdown========================");
    }
}

再定义一个自定义的MySpringBeanPostProcessor,主要是重写了BeanPostProcessor接口的postProcessBeforeInitialization与postProcessAfterInitialization方法。

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 *
 * @author runtu
 * @version MySpringBeanPostProcessor, v2.0 2019/12/13 9:25
 **/
@Component
public class MySpringBeanPostProcessor implements BeanPostProcessor {

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

        if (bean instanceof TestBean) {
            System.out.println(
                "=====================postProcessBeforeInitialization========================");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean,
                                                 String beanName) throws BeansException {
        if (bean instanceof TestBean) {
            System.out.println(
                "=====================postProcessAfterInitialization========================");
        }
        return bean;
    }
}

此处我工程为springboot,把TestBean注入之后,启动项目

@Bean(initMethod = "init", destroyMethod = "shutdown")
public TestBean testBean() {
     return new TestBean();
}

把一些无关的日志剃掉之后,控制台打印结果如下:

# 启动程序之后控制台依次打印:

=====================构造方法========================
=====================BeanNameAware========================
=====================BeanFactoryAware========================
=====================ApplicationContextAware========================
=====================postProcessBeforeInitialization========================
=====================InitializingBean========================
=====================init========================
=====================postProcessAfterInitialization========================

# 停止程序之后依次打印:
=====================DisposableBean========================
=====================shutdown========================

over。。。。

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页