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


@Nullableprotected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {Object result = bp.postProcessBeforeInstantiation(beanClass, beanName);if (result != null) {return result;}}return null;}这不就是对所有的InstantiationAwareBeanPostProcessor进行遍历调用吗 , 取第一个返回结果的Processor的结果 。大家说,这里用到了什么设计模式??
InstantiationAwareBeanPostProcessor before hook的使用#下面 , 我们自己创建一个实现类,它的功能就是打印所有进来的Bean名字和类型:
public class MyInstantiationProcessor implements InstantiationAwareBeanPostProcessor {@Overridepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {System.out.println("[+] > MyInstantiationProcessor before bean instantiation : " + beanName + " => " + beanClass.getName());return null;}}【从BeanFactory源码看Bean的生命周期】下面,我们把它设置到BeanFactory中,然后尝试获取Bean:
factory.addBeanPostProcessor(new MyInstantiationProcessor());Workbench workbench = factory.getBean(Workbench.class);System.out.println(workbench);输出:
[+] > 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=Yudoge))因为Workbench对象依赖Person对象,所以引起了两个对象的连锁创建 , 最后一行我们得到了Workbench对象 。
下面我们尝试让BeanPostProcessor针对Person类返回一个另外的Bean,而不是null
public class MyInstantiationProcessor implements InstantiationAwareBeanPostProcessor {@Overridepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {System.out.println("[+] > MyInstantiationProcessor before bean instantiation : " + beanName + " => " + beanClass.getName());if (beanClass.getName().equals(Person.class.getName())) {return new Person("我是MyInstantiationProcessor返回的Person");}return null;}}运行:
[+] > 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=我是MyInstantiationProcessor返回的Person))成功的返回了我们创建出来的Person 。
对目前的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方法#

推荐阅读