本文先只分析从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销毁时调用