从BeanFactory源码看Bean的生命周期( 五 )

结果,由于autowire阶段Person已经被创建了,所以结果中person的实例化阶段也被打印了:
[+] > MyInstantiationProcessor before bean instantiation : workbench => top.yudoge.springserials.basic.beanfactory.beans.Workbench[+] > MyInstantiationProcessor before bean instantiation : person => top.yudoge.springserials.basic.beanfactory.beans.PersonWorkbench(operator=Person(name=修改了pvs之后的person))

由于我不了解直接修改原始pvs会不会有副作用,所以我选择了克隆一个对象
这两个阶段最后的总结#回到这个图上
从BeanFactory源码看Bean的生命周期

文章插图
  1. 从BeanDefinition创建实例化Bean
  2. 实例化之前如果有InstantiationAwareBeanPostProcessor,调用before实例化方法
  3. 如果并没有一个实例化BeanPostProcessor接管Bean创建,那么进入正常实例化阶段
  4. 实例化Bean
  5. 调用InstantiationAwareBeanPostProcessor的after实例化方法
  6. 对pvs进行设置
  7. 调用InstantiationAwareBeanPostProcessor的postProcessProperties方法对pvs进行修改
  8. 实际的Bean属性设置
初始化阶段#看起来挺复杂的 , 其实很简单:
从BeanFactory源码看Bean的生命周期

文章插图
主要分为几个阶段:
  1. Aware接口的回调
  2. BeanPostProcessor的before初始化回调
  3. 各种初始化方法的回调
  4. BeanPostProcessor的after初始化回调
所以 , 初始化阶段其实没做任何实际的事,只是对各种生命周期方法和Bean感知方法进行回调,通知它们Bean已经初始化了 。或者也可以理解为,初始化阶段做的最主要的工作就是调用Bean的aware感知方法和初始化方法
// doCreateBean// 属性设置阶段populateBean(beanName, mbd, instanceWrapper);// 初始化阶段exposedObject = initializeBean(beanName, exposedObject, mbd);进入initalizeBean,可以看到里面的代码正对应着图中的每一步:
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {// 调用感知方法invokeAwareMethods(beanName, bean);// 调用BeanPostProcessor的before初始化方法Object wrappedBean = bean;if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}// 调用自定义初始化方法try {invokeInitMethods(beanName, wrappedBean, mbd);}// 调用BeanPostProcessor的after初始化方法catch (Throwable ex) {}if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);}return wrappedBean;}Aware感知方法的调用#进入invokeAwareMethod方法,里面对BeanNameAwareBeanClassLoaderAwareBeanFactoryAware做了检测,并调用了对应的设置方法:
private void invokeAwareMethods(String beanName, Object bean) {if (bean instanceof Aware) {if (bean instanceof BeanNameAware) {((BeanNameAware) bean).setBeanName(beanName);}if (bean instanceof BeanClassLoaderAware) {ClassLoader bcl = getBeanClassLoader();if (bcl != null) {((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);}}if (bean instanceof BeanFactoryAware) {((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);}}}对于BeanFactory,它只支持这些Aware,ApplicationContext会支持更多的Aware 。
Aware感知方法的实战#让Workbench类实现BeanFactoryAware接口并打印出创建它的BeanFactory:
@Datapublic class Workbench implements BeanFactoryAware {@Autowiredprivate Person operator;@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {System.out.println("BeanFactory => " + beanFactory);}}
从BeanFactory源码看Bean的生命周期

文章插图
需要注意的是,一旦你的Bean实现了某个Aware接口,就证明它要感知到某些框架中的东西 , 这会让它直接与框架产生耦合 。
BeanPostProcessor before初始化的调用#这里和之前的套路一样,并且before方法可以返回一个包装过的Bean做为代理(默认不包装,这时wrappedBean==bean)
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {invokeAwareMethods(beanName, bean);// 进行before初始化调用Object wrappedBean = bean;if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}// ...}遍历每个BeanPostProcessor , 调用before初始化方法:
@Overridepublic Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)throws BeansException {Object result = existingBean;for (BeanPostProcessor processor : getBeanPostProcessors()) {Object current = processor.postProcessBeforeInitialization(result, beanName);if (current == null) {return result;}result = current;}return result;}

推荐阅读