getBean创建bean的过程

1. 调用doGetBean方法,进入doGetBean方法

2. 首先从缓存中获取bean,如果有则获取返回

// Eagerly check singleton cache for manually registered singletons.
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
    if (logger.isDebugEnabled()) {
        if (isSingletonCurrentlyInCreation(beanName)) {
            logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
                    "' that is not fully initialized yet - a consequence of a circular reference");
        }
        else {
            logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
        }
    }
    bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}

注:这个机制保证了单实例bean只会被创建一次,查看 getSingleton 就会发现是从Map singletonObjects 中获取的

3. 如果缓存中没有,就走创建bean的流程(略过一些代码,只关注单实例bean的创建)

  1. 标记bean创建了
    if (!typeCheckOnly) {
     markBeanAsCreated(beanName);
    }
    
  2. 获取bean定义,且判断是否有依赖的bean,如果有先创建依赖的bean
    final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
    checkMergedBeanDefinition(mbd, beanName, args);
    // Guarantee initialization of beans that the current bean depends on.
    String[] dependsOn = mbd.getDependsOn();
    if (dependsOn != null) {
        for (String dep : dependsOn) {
            if (isDependent(beanName, dep)) {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                    "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
            }
            registerDependentBean(dep, beanName);
            getBean(dep);
        }
    }
    
    注:此处正是dependsOn属性,即@DependsOn在源码处的体现
  3. 如果是单实例bean就走createBean创建bean
    1. 解析bean,resolveBeanClass(mbd, beanName);
    2. 给bean的后置处理器一个机会创建bean的代理对象 - 即InstantiationAwareBeanPostProcessor提前执行postProcessBeforeInstantiation
      // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
      Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
      
      注:使用切面后可以关注此方法
    3. 创建bean,进入doCreateBean
      1. 创建bean实例instanceWrapper = createBeanInstance(beanName, mbd, args);
        • 如果有工厂方法,则使用构造方法或工厂方法创建bean instantiateUsingFactoryMethod(beanName, mbd, args);
      2. MergedBeanDefinitionPostProcessor回调postProcessMergedBeanDefinition合并bean定义applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
      3. 为bean的属性赋值populateBean(beanName, mbd, instanceWrapper);
        • 处理实现InstantiationAwareBeanPostProcessor接口的后置处理器回调postProcessAfterInstantiation
        • 处理实现InstantiationAwareBeanPostProcessor接口的后置处理器回调postProcessPropertyValues
        • 注:InstantiationAwareBeanPostProcessor接口至此三个方法都执行了
      4. 初始化beanexposedObject = initializeBean(beanName, exposedObject, mbd);
        • 执行Aware 方法 invokeAwareMethods(beanName, bean);
        • applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);执行后置处理器初始化之前的方法 其实就是循环回调方法
        • invokeInitMethods(beanName, wrappedBean, mbd);执行初始化方法
          是InitializingBean接口的实现,则实现接口规定的初始化
          有自定义初始化 执行自定义初始化的方法
        • applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);执行后置处理器初始化之后的方法
      5. registerDisposableBeanIfNecessary(beanName, bean, mbd);注册bean销毁方法
    4. getSingletonaddaddSingleton(beanName, singletonObject);将单实例bean加入缓存中
  4. 所有bean创建完成后,检查所有的bean是否是SmartInitializingSingleton
    1. 如果是执行afterSingletonsInstantiated

你可能感兴趣的:(getBean创建bean的过程)