Spring Bean生命周期相关接口分析与调用时机

本文先只分析从getBean开始的流程,ApplicationContext相关暂不多说。不过这里顺便多写一点,关注一下BeanDefinition的注册时机。Spring做为bean工厂的重要方法在ConfigurableApplicationContext.refresh()里,抽象类AbstractApplicationContext定义了调用模板,其中有一个obtainFreshBeanFactory(),这里主要有两条分支:

1、AbstractRefreshableApplicationContext.refreshBeanFactory()

字面理解,可刷新的context,每次调用会重新销毁旧的&创建新DefaultListableBeanFactory,并且loadBeanDefinition进行注册;比如在tomcat中运行的spring mvc就是这个。

2、GenericApplicationContext.refreshBeanFactory()

只是简单打一个标记,只能刷新一次,否则抛异常;比如SpringBoot应用的Context都是其子类,注册BeanDefinition是在refresh之前,由BeanDefinitionLoader完成。

----------------------

1)、bean生命周期相关接口

1、BeanPostProcessor

比如@PostConstruct由CommonAnnotationBeanPostProcessor.postProcessBeforeInitialization处理,先于初始化方法

public interface BeanPostProcessor {
    //⑴
    //bean实例化、依赖注入后,初始化之前调用
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    //⑵
    //bean初始化后调用
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

2、InstantiationAwareBeanPostProcessor 

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
    //⑴
    //通常的实例化之前,即可返回bean
    default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        return null;
    }

    //⑵
    //populateBean()里,通常的注入PropertyValues之前,就可进行操作,返回false就注入并返回
    default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        return true;
    }

    //这里是依赖注入相关
    //PropertyValue=PropertyValues.getPropertyValues()
    //PropertyValue即依赖的bean的name、value键值对信息

    //⑶
    //设置了PropertyValues,解析为运行时引用之前,可以修改pvs,返回null后不进行applyPropertyValues,一般返回处理后的pvs
    default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        return null;
    }

    @Deprecated
    //废弃接口,功能同⑶

}

3、Aware

public interface Aware {
    //标记接口,initializeBean中只设置Bean相关的三种
    //BeanNameAware
    //BeanClassLoaderAware
    //BeanFactoryAware
}

4、初始化方法,顺序:①InitializingBean、②init-method

—① 

public interface InitializingBean {
    //调用在1-⑴、1-⑵之间
    void afterPropertiesSet() throws Exception;
}

② RootBeanDefinition.getInitMethodName(),紧接着

5、bean销毁:

@PreDestroy,DisposableBean,destroy-method,最终都是由在doCreateBean中最后对bean注册的

public interface DisposableBean {
    void destroy() throws Exception;
}

适配器DisposableBeanAdapter实现

class DisposableBeanAdapter implements DisposableBean, ... {
    ...
    @Override
    public void destroy() {
        //1、DestructionAwareBeanPostProcessor实现CommonAnnotationBeanPostProcessor
        //   调用@PreDestroy标记方法

        //2、((DisposableBean) this.bean).destroy();

        //3、destroy-method
    }
    ...
}

2)、调用顺序

理解了接口就能理解出bean的生命周期,围绕着实例化依赖注入,从上到下:

  • -----------------实例化-----------------------
  • 2-⑴ postProcessBeforeInstantiation:实例化之前尝试生产bean(通常是代理),如果返回非空则调用1-⑵,依然返回非空就拿到getBean
  • 【实例化bean】
  • ----------------依赖注入----------------------
  • 2-⑵ postProcessAfterInstantiation:注入pvs之前进行处理
  • 【注入PropertyValues】
  • 2-⑶ postProcessProperties:处理pvs
  • 【处理pvs,实现真正的注入】
  • ----------------初始化-------------------------
  • 3 Aware:设置aware
  • 1-⑴ postProcessBeforeInitialization:初始化前
  • 4 afterPropertiesSet:初始化调用
  • 1-⑵ postProcessAfterInitialization:初始化后
  • -----------------注册销毁---------------------
  • 5 destroy:bean销毁时调用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值