SpringBean实例化对象

阅读更多

1. Spring Bean实例化

1. 通过BeanName获取Class对象(resolveBeanClass(mbd, beanName);)

2. 处理lookup标签(mbdToUse.prepareMethodOverrides();)// Prepare method overrides.

3. 短路Bean的创建过程 Object bean = resolveBeforeInstantiation(beanName, mbdToUse);

// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.

Object bean = resolveBeforeInstantiation(beanName, mbdToUse);

if (bean != null) {

    return bean;

}

 

Class targetType = determineTargetType(beanName, mbd);

if (targetType != null) {

    bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);

    if (bean != null) {

        bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);

    }

}

    

protected Object applyBeanPostProcessorsBeforeInstantiation(Class beanClass, String beanName) {

    for (BeanPostProcessor bp : getBeanPostProcessors()) {

        if (bp instanceof InstantiationAwareBeanPostProcessor) {

            InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;

            Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);

            if (result != null) {

                return result;

            }

        }

    }

    return null;

}

 

public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)

        throws BeansException {

 

    Object result = existingBean;

    for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {

        result = beanProcessor.postProcessAfterInitialization(result, beanName);

        if (result == null) {

            return result;

        }

    }

    return result;

}

4. 实例化对象(doCreateBean(beanName, mbdToUse, args);)

  • 实例化对象(instanceWrapper = createBeanInstance(beanName, mbd, args))
    • 使用FactoryMethod实例化(instantiateUsingFactoryMethod(beanName, mbd, args);)
    • 解析过构造参数或FactoryMethod,才使用构造参数实例化(autowireConstructor(beanName, mbd, null, null);)
    • 使用策略实例化(instantiateBean(beanName, mbd);)InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
    • if (bd.getMethodOverrides().isEmpty()) {
    •     // 反射初始化
    •     return BeanUtils.instantiateClass(constructorToUse);
    • } else {
    •     // Must generate CGLIB subclass.
    •     return instantiateWithMethodInjection(bd, beanName, owner);
    • }
    •  
  • 提前曝光实例(addSingletonFactory),循环依赖使用
  • 填充属性(populateBean(beanName, mbd, instanceWrapper);)
    • 短路属性注入 bp instanceof InstantiationAwareBeanPostProcessor / !ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)if (bp instanceof InstantiationAwareBeanPostProcessor) {
    •     InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
    •     if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
    •         continueWithPropertyPopulation = false;
    •         break;
    •     }
    • }
    •  
    • 属性注入,按照名称(autowireByName(beanName, mbd, bw, newPvs);)或类型(autowireByType(beanName, mbd, bw, newPvs);)注入
    • 短路if (bp instanceof InstantiationAwareBeanPostProcessor) {
    •             InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
    •             pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
    •             if (pvs == null) {
    •                 return;
    •             }
    • }
    •  
    • 应用属性(applyPropertyValues(beanName, mbd, bw, pvs);Spel表达式解析)
  • 初始化Bean(initializeBean(beanName, exposedObject, mbd);)
    • 执行Aware接口自动注入,包括BeanNameAware、BeanFactoryAware、BeanClassLoaderAware(invokeAwareMethods(beanName, bean);)
    • 短路bean后续BeanPostProcessor执行。postProcessBeforeInitializationresult = beanProcessor.postProcessBeforeInitialization(result, beanName);
    • if (result == null) {
    •     return result;
    • }
    •  
    • 执行init初始化方法(invokeInitMethods(beanName, wrappedBean, mbd);)
      • ((InitializingBean) bean).afterPropertiesSet();
      • invokeCustomInitMethod(beanName, bean, mbd);
    • 短路bean后续BeanPostProcessor执行。wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
    •     result = beanProcessor.postProcessAfterInitialization(result, beanName);
    •     if (result == null) {
    •         return result;
    •     }
    • }
    •  

5. 初始化依赖的Bean

6. Register bean as disposable.(registerDisposableBeanIfNecessary(beanName, bean, mbd);)

 

  • SpringBean实例化对象_第1张图片
  • 大小: 1.9 MB
  • 查看图片附件

你可能感兴趣的:(Spring)