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


对目前的InstantiationAwareBeanPostProcessor的before阶段做一个总结:

  1. BeanFactory会在创建Bean之前调用所有这种Processor的before方法
  2. 如果在before中返回了一个对象,那么这个对象就会代替原来的Bean,并且该bean的初始化后(不是实例化后)方法会被立即调用
  3. 否则,就是before中返回null,这时进入正常的Bean创建流程
这个总结只是AbstractAutowireCapableBeanFactory中的实现方式,是否有其它BeanFactory以其它方式实现,暂不明确
实际实例化Bean#所以,当InstantiationAwareBeanPostProcessor没有返回一个替代对象时,进入正常的Bean创建流程,开始实例化Bean 。
回到AbstractAutowireBeanFactorycreateBean方法:
@Overrideprotected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {RootBeanDefinition mbdToUse = mbd;try {Object bean = resolveBeforeInstantiation(beanName, mbdToUse);if (bean != null) {return bean;}}catch (Throwable ex) { }try {// 创建BeanObject beanInstance = doCreateBean(beanName, mbdToUse, args);return beanInstance;}catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {throw ex;}catch (Throwable ex) {}}这个doCreateBean方法就是用来实际实例化Bean的:
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {BeanWrapper instanceWrapper = null;// 如果是Singleton,从缓存中拿原来的Beanif (mbd.isSingleton()) {instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);}if (instanceWrapper == null) {// 实例化BeaninstanceWrapper = createBeanInstance(beanName, mbd, args);}// 从BeanWrapper中拿出实际的Bean对象Object bean = instanceWrapper.getWrappedInstance();// ...}向populateBean方法中试探——InstantiationAwareBeanPostProcessor的after方法#doCreateBean方法里有这样两行:
populateBean(beanName, mbd, instanceWrapper);exposedObject = initializeBean(beanName, exposedObject, mbd);很明显,它们代表了Bean生命周期的属性设置阶段和初始化阶段,但是到目前为止,我们还没有看到InstantiationAwareBeanPostProcessor的除了before以外的另两个方法被调用 。虽然populateBean方法名看起来就是设置Bean属性了,但我们也只能往下看,没准InstantiationAwareBeanPostProcessor的另外两个方法在这个populateBean设置属性之前被调用了呢?
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {// 调用所有InstantiationAwareBeanPostProcessor的BeanPostProcessorif (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {return;}}}// ...}我日尼玛,果然啊!!!进来就开始调用InstantiationAwareBeanPostProcessor的after方法
所以,做个小总结:
  1. 对于那些InstantiationAwareBeanPostProcessor没有拦截(before实例化方法返回null的)的Bean,InstantiationAwareBeanPostProcessor的after实例化方法也会被调用
  2. 而对于那些before实例化方法没有返回null的,这个after实例化方法不会走,直接调用了after初始化方法,也就是说把实例化到初始化中间的过程都跳过了
InstantiationAwareBeanPostProcessor的postProcessProperties方法#protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {// 调用after实例化方法if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {return;}}}// 获取所有属性PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);// 自动注入,自动注入是针对pvs的,而不是bean本身int resolvedAutowireMode = mbd.getResolvedAutowireMode();if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {MutablePropertyValues newPvs = new MutablePropertyValues(pvs);// Add property values based on autowire by name if applicable.if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {autowireByName(beanName, mbd, bw, newPvs);}// Add property values based on autowire by type if applicable.if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {autowireByType(beanName, mbd, bw, newPvs);}pvs = newPvs;}// 对于所有的InstantiationAwareBeanPostProcessor,调用它们的属性设置方法boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);PropertyDescriptor[] filteredPds = null;if (hasInstAwareBpps) {if (pvs == null) {pvs = mbd.getPropertyValues();}// 对于所有InstantiationAwareBeanPostProcessor,调用它的postProcessProperties方法for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {PropertyValues pvsToUse = bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);// 如果该方法返回的新pvs为null,那么再调用postProcessPropertyValuesif (pvsToUse == null) {// 该方法默认返回初始pvspvsToUse = bp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);if (pvsToUse == null) {return;}}// 让pvs等于两次BeanProcessor方法调用后返回的pvspvs = pvsToUse;}}// 实际设置Bean属性if (pvs != null) {applyPropertyValues(beanName, mbd, bw, pvs);}}

经验总结扩展阅读