11_初始化剩下的单实例bean

查看全部
// Instantiate all remaining (non-lazy-init) singletons. 初始化剩下的单实例bean finishBeanFactoryInitialization(beanFactory);

  1. 初始化容器中的类转换器,id 为conversionService
    // Initialize conversion service for this context.
    if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
        beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME,  ConversionService.class)) {
        beanFactory.setConversionService(
        beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
    }
    
  2. 注册值解析器
    // Register a default embedded value resolver if no bean post-processor
    // (such as a PropertyPlaceholderConfigurer bean) registered any before:
    // at this point, primarily for resolution in annotation attribute values.
    if (!beanFactory.hasEmbeddedValueResolver()) {
        beanFactory.addEmbeddedValueResolver(new StringValueResolver() {
            @Override
            public String resolveStringValue(String strVal) {
                return getEnvironment().resolvePlaceholders(strVal);
            }
        });
    }
    
  3. AspectJ相关
    // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
    String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
    for (String weaverAwareName : weaverAwareNames) {
        getBean(weaverAwareName);
    }
    
  4. 对beanFactory做一些配置
    // Stop using the temporary ClassLoader for type matching.
    // 停止使用临时的ClassLoader进行类型匹配。
    beanFactory.setTempClassLoader(null);
    
    // Allow for caching all bean definition metadata, not expecting further changes.
    //允许缓存所有bean定义信息
    beanFactory.freezeConfiguration();
    
  5. 初始化剩余的单实例bean
    // Instantiate all remaining (non-lazy-init) singletons.
    beanFactory.preInstantiateSingletons();
    
    1. 获得所有的bean name
      List beanNames = new ArrayList(this.beanDefinitionNames);
      
    2. 循环遍历beanNames
      • 获取bean定义
      RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
      
      • 初始化的bean必须满足的条件 - 非抽象类&&是单实例的&&非懒加载
      • bean如果实现了FactoryBean,则使用FactoryBean的getBean创建bean;否则使用getBean创建bean
      for (String beanName : beanNames) {
          RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
          if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
              if (isFactoryBean(beanName)) {
                  final FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
                  boolean isEagerInit;
                  if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                      isEagerInit = AccessController.doPrivileged(new PrivilegedAction() {
                          @Override
                          public Boolean run() {
                              return ((SmartFactoryBean) factory).isEagerInit();
                          }
                      }, getAccessControlContext());
                  }
                  else {
                      isEagerInit = (factory instanceof SmartFactoryBean &&
                              ((SmartFactoryBean) factory).isEagerInit());
                  }
                  if (isEagerInit) {
                      getBean(beanName);
                  }
              }
              else {
                  getBean(beanName);
              }
          }
      }
      
      注:getBean创建bean的过程
    3. 再次循环遍历,如果实现了SmartInitializingSingleton接口,则回调afterSingletonsInstantiated

你可能感兴趣的:(11_初始化剩下的单实例bean)