深入理解 Spring finishBeanFactoryInitialization (4)

经过上面的标记,在哪里产生的代理对象呢?其实是在AbstractAutowireCapeableBeanFactory中的initializeBean()方法中实现的postProcessAfterInitialization()实现的,在本文的末尾展开讨论

第二件大事: 实例化对象, 继续跟进

protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { if (logger.isDebugEnabled()) { logger.debug("Creating instance of bean \'" + beanName + "\'"); } RootBeanDefinition mbdToUse = mbd; // Make sure bean class is actually resolved at this point, and // clone the bean definition in case of a dynamically resolved Class which cannot be stored in the shared merged bean definition. // todo 做各种各样的属性值的赋值, 比如这种 通过Spring的Bean传递给Spring框架的值 ==> bd.setPropertyValue("aaa") Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); } // Prepare method overrides. // todo 处理 lookup-method 和 replace-method 配置,Spring 将这两个配置统称为 override method try { mbdToUse.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { // todo 在实例化之前完成一次解析操作,这也是 Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", ex); } try { //todo 调用 doCreateBean 创建bean Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean \'" + beanName + "\'"); } return beanInstance; } }

我们继续跟进doCreateBean(beanName, mbdToUse, args);方法, 同样是本类AbstarctAutowireCapableBeanFactory的方法,源码如下: 这个方法也是无与伦比的重要,那这个方法中做了什么事情呢?如下

创建一个 BeanWrapper,用来存放bean+其他属性

创建bean的实例,封装进上面的BeanWrapper中

分两次调用处理处理器

设置属性,填充属性

经过AOP处理,将原生对象转换成Proxy

返回BeanWrapper

因为这个方法简直太重要了,上面列举的每一点都值得我们仔细分析,我们每一条的分析都写在下面代码的下面

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { // Instantiate the bean. // todo BeanWrapper 用来包装bean BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { // todo 一开始 factoryBeanInstanceCache 这个map中是没有值的, 所以进入下面的if instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mbd, args); } // todo !!!!!!!!这里获取出来的对象是原生对象!!!!!!!!!!!! final Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; } // Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { try { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Post-processing of merged bean definition failed", ex); } mbd.postProcessed = true; } } // Eagerly cache singletons to be able to resolve circular references // even when triggered by lifecycle interfaces like BeanFactoryAware. boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if (earlySingletonExposure) { if (logger.isDebugEnabled()) { logger.debug("Eagerly caching bean \'" + beanName + "\' to allow for resolving potential circular references"); } addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); } // Initialize the bean instance. Object exposedObject = bean; // todo 到目前为止还是原生对象 //todo 用来填充属性 //设置属性,非常重要 populateBean(beanName, mbd, instanceWrapper); // todo 经过AOP处理,原生对象转换成了代理对象,跟进去 //执行后置处理器,aop就是在这里完成的处理 exposedObject = initializeBean(beanName, exposedObject, mbd); return exposedObject; }

实例化对象

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwpfgs.html