回顾

Spring 容器初始化流程(中)中我们分析了bean工厂的后置处理器注册调用、Bean后置处理器的注册、国际化、事件广播器等 ,在最后的篇幅中,我们将会分析上下文的监听器注册,及容器生命周期等相关源码。在最后还会附上一张较为完整的容器初始化流程图哦~

开始吧~

10. registerListeners

在所有 beanDefinition 中查找 listener bean,然后注册到广播器中

// AbstractApplicationContext.java

protected void registerListeners() {
    // 首先注册静态指定的侦听器。
    for (ApplicationListener<?> listener : getApplicationListeners()) {
        getApplicationEventMulticaster().addApplicationListener(listener);
    }

    // Do not initialize FactoryBeans here: We need to leave all regular beans
    // uninitialized to let post-processors apply to them!
    // 不要在这里初始化 FactoryBean:需要将所有常规 bean 保持未初始化状态,以便后处理器适用于它们!
    String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, truefalse);
    for (String listenerBeanName : listenerBeanNames) {
        getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
    }

    // 至此,已经完成将监听器注册到ApplicationEventMulticaster中,下面将发布前期的事件给监听器。
    Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
    this.earlyApplicationEvents = null;
    if (earlyEventsToProcess != null) {
        for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
            getApplicationEventMulticaster().multicastEvent(earlyEvent);
        }
    }
}
11. finishBeanFactoryInitialization

初始化剩下的单例Bean(非延迟加载的)

流程如下:

然后来看看源码:

// AbstractApplicationContext.java

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
    // 初始化转换器
    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));
    }

    // 如果之前没有注册 bean 后置处理器(例如PropertyPlaceholderConfigurer),则注册默认的解析器
    if (!beanFactory.hasEmbeddedValueResolver()) {
        beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
    }

    // 初始化LoadTimeWeaverAware类型的bean LoadTimeWeaverAware是类加载时进行织入
    String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, falsefalse);
    for (String weaverAwareName : weaverAwareNames) {
        getBean(weaverAwareName);
    }

    // 停止使用临时类加载器进行类型匹配。
    beanFactory.setTempClassLoader(null);

    // 冻结之前beandifinition的配置,留作后面的缓存用
    beanFactory.freezeConfiguration();

    // 初始化所有剩余的单例(非延迟初始化)
    beanFactory.preInstantiateSingletons();
}
11.1 `LoadTimeWeaverAware`

Spring使用LoadTimeWeaver在将类加载到Java虚拟机(JVM)时动态转换类。

11.2 `freezeConfiguration`

只是更新容器的配置状态和BeanDefinitionNames缓存

// DefaultListableBeanFactory.java

/** 是否可以缓存所有bean的bean定义元数据。 */
private volatile boolean configurationFrozen = false;

/** 缓存的bean定义名数组。 */
@Nullable
private volatile String[] frozenBeanDefinitionNames;

@Override
public void freezeConfiguration() {
    this.configurationFrozen = true;
    this.frozenBeanDefinitionNames = StringUtils.toStringArray(this.beanDefinitionNames);
}
11.3 `preInstantiateSingletons`

初始化所有剩余的单例(非延迟初始化),结合上面的流程图和注释

// DefaultListableBeanFactory.java

@Override
public void preInstantiateSingletons() throws BeansException {
    if (logger.isTraceEnabled()) {
        logger.trace("Pre-instantiating singletons in " + this);
    }

    // 迭代副本以允许 init 方法,这些方法反过来注册新的 Bean 定义。
    List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

    // 初始化所有非lazy init的bean
    for (String beanName : beanNames) {
        RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
        if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
            // 非抽象的单例类,且lazy init为false
            if (isFactoryBean(beanName)) {
                // 工厂bean
                Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                if (bean instanceof FactoryBean) {
                    final FactoryBean<?> factory = (FactoryBean<?>) bean;
                    boolean isEagerInit;
                    // 判断工厂bean是否需要优先初始化
                    if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                        isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
                                        ((SmartFactoryBean<?>) factory)::isEagerInit,
                                getAccessControlContext());
                    }
                    else {
                        isEagerInit = (factory instanceof SmartFactoryBean &&
                                ((SmartFactoryBean<?>) factory).isEagerInit());
                    }
                    if (isEagerInit) {
                        // 初始化工厂bean
                        getBean(beanName);
                    }
                }
            }
            else {
                // 非工厂bean,直接初始化
                getBean(beanName);
            }
        }
    }

    // 为所有适用的bean触发初始化后的回调…
    for (String beanName : beanNames) {
        // 获取单例实例
        Object singletonInstance = getSingleton(beanName);
        if (singletonInstance instanceof SmartInitializingSingleton) {
            // 如果是 SmartInitializingSingleton 类型的实例,则调用其单例初始化后的回调方法
            final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                    smartSingleton.afterSingletonsInstantiated();
                    return null;
                }, getAccessControlContext());
            }
            else {
                smartSingleton.afterSingletonsInstantiated();
            }
        }
    }
}

11.3.1 getSingleton

这里是真正初始化的逻辑:

// DefaultListableBeanFactory.java

@Override
@Nullable
public Object getSingleton(String beanName) {
    return getSingleton(beanName, true);
}

@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
    // 先从单例缓存中取,取到就直接返回
    Object singletonObject = this.singletonObjects.get(beanName);
    if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
        // 如果取不到,且单例正在初始化中
        synchronized (this.singletonObjects) {
            // 从早期初始化的缓存中取(在loadBeanDefinition的时候可能触发了并完成了实例化)
            singletonObject = this.earlySingletonObjects.get(beanName);
            if (singletonObject == null && allowEarlyReference) {
                // 如果没取到,并且允许从早期引用中获取(在loadBeanDefinition时触发但未完成实例化)
                ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                if (singletonFactory != null) {
                    // 如果能获取到早期的对象工厂,就调用对象工厂的getObject方法获取单例实例
                    singletonObject = singletonFactory.getObject();
                    // 设置到早期单例缓存对象中,以便其他依赖bean获取
                    this.earlySingletonObjects.put(beanName, singletonObject);
                    // 移除早期的单例对象工厂
                    this.singletonFactories.remove(beanName);
                }
            }
        }
    }
    return singletonObject;
}

结合注释,比较清楚的逻辑

12. finishRefresh

完成刷新过程,通知生命周期处理器 lifecycleProcessor 刷新过程,同时发出 ContextRefreshEvent 通知别人

主要是调用 LifecycleProcessor#onRefresh() ,并且发布事件(ContextRefreshedEvent)。

// AbstractApplicationContext.java

protected void finishRefresh() {
    // 清除上下文级资源缓存(如扫描中的 ASM 元数据)。
    clearResourceCaches();

    // 初始化上下文的生命周期处理器。
    initLifecycleProcessor();

    // 通知实现了Lifecycle的bean容器已经完成刷新
    getLifecycleProcessor().onRefresh();

    // 发布刷新完成事件。
    publishEvent(new ContextRefreshedEvent(this));

    // 如果配置了pring.liveBeansView.mbeanDomain属性,就尝试注册到JVM平台的MBeanServer中
    // 以便通过JXM来实时查看上下文中注册了哪些bean
    LiveBeansView.registerApplicationContext(this);
}
12.1 `clearResourceCaches`
// AbstractApplicationContext.java

public void clearResourceCaches() {
    this.resourceCaches.clear();
}
12.2 `initLifecycleProcessor`
// AbstractApplicationContext.java

protected void initLifecycleProcessor() {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
        // 如果有配置了生命周期处理器,则实例化并注册到容器中
        this.lifecycleProcessor =
                beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
        if (logger.isTraceEnabled()) {
            logger.trace("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");
        }
    }
    else {
        // 如果没有指定,则实例化默认的生命周期处理器,并注册到容器和bean工厂中(注册为单例bean)
        DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
        defaultProcessor.setBeanFactory(beanFactory);
        this.lifecycleProcessor = defaultProcessor;
        beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
        if (logger.isTraceEnabled()) {
            logger.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "' bean, using " +
                    "[" + this.lifecycleProcessor.getClass().getSimpleName() + "]");
        }
    }
}

12.3 getLifecycleProcessor#onRefresh

已默认的生命周期处理器来示例

// DefaultLifecycleProcessor.java

@Override
public void onRefresh() {
    // 调用所有生命周期的start方法
    startBeans(true);
    // 设置订单的running状态为true
    this.running = true;
}

12.3.1 startBeans方法

对生命周期分组并排序后,调用其start方法

// DefaultLifecycleProcessor.java

private void startBeans(boolean autoStartupOnly) {
    // 获取所有Lifecycle类型的bean
    Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
    Map<Integer, LifecycleGroup> phases = new HashMap<>();
    // 对不同阶段的bean进行遍历分组
    lifecycleBeans.forEach((beanName, bean) -> {
        if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
            int phase = getPhase(bean);
            LifecycleGroup group = phases.get(phase);
            if (group == null) {
                group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
                phases.put(phase, group);
            }
            group.add(beanName, bean);
        }
    });
    // 如果有则调用生命周期组的start方法
    if (!phases.isEmpty()) {
        List<Integer> keys = new ArrayList<>(phases.keySet());
        Collections.sort(keys);
        for (Integer key : keys) {
            phases.get(key).start();
        }
    }
}
  • getLifecycleBeans

获取所有Lifecycle类型的bean

// DefaultLifecycleProcessor.java

protected Map<String, Lifecycle> getLifecycleBeans() {
    // 获取bean工厂
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    Map<String, Lifecycle> beans = new LinkedHashMap<>();
    // 获取 Lifecycle 类型的beanName
    String[] beanNames = beanFactory.getBeanNamesForType(Lifecycle.class, falsefalse);
    for (String beanName : beanNames) {
        // 获取真实的beanName(可能传入的是工厂bean或者alias)
        String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName);
        boolean isFactoryBean = beanFactory.isFactoryBean(beanNameToRegister);
        String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
        if ((beanFactory.containsSingleton(beanNameToRegister) &&
                (!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) ||
                matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) {
            // 如果是已注册的单例bean,或者符合指定类型的bean,则进行初始化并放到Map中返回
            Object bean = beanFactory.getBean(beanNameToCheck);
            if (bean != this && bean instanceof Lifecycle) {
                beans.put(beanNameToRegister, (Lifecycle) bean);
            }
        }
    }
    return beans;
}
  • LifecycleGroup#start

    // DefaultLifecycleProcessor.java

    public void start() {
      // members就是上面通过group.add方法添加进来的
      if (this.members.isEmpty()) {
          return;
      }
      if (logger.isDebugEnabled()) {
          logger.debug("Starting beans in phase " + this.phase);
      }
      Collections.sort(this.members);
      for (LifecycleGroupMember member : this.members) {
          doStart(this.lifecycleBeans, member.name, this.autoStartupOnly);
      }
    }

    实际上就是调用成员的start方法,先来看看LifecycleGroupMember的结构

    // DefaultLifecycleProcessor.java

    private class LifecycleGroupMember implements Comparable<LifecycleGroupMember{

      private final String name;

      private final Lifecycle bean;
    }
  • 只是简单的beanName及生命周期bean实例

    再看看doStart方法

    // DefaultLifecycleProcessor.java

    private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
      Lifecycle bean = lifecycleBeans.remove(beanName);
      if (bean != null && bean != this) {
          // 当前bean不是自己
          // 获取bean的依赖,并递归调用doStart方法
          String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
          for (String dependency : dependenciesForBean) {
              doStart(lifecycleBeans, dependency, autoStartupOnly);
          }
          if (!bean.isRunning() &&
                  (!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
              if (logger.isTraceEnabled()) {
                  logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
              }
              try {
                  // 启动生命周期
                  bean.start();
              }
              catch (Throwable ex) {
                  throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
              }
              if (logger.isDebugEnabled()) {
                  logger.debug("Successfully started bean '" + beanName + "'");
              }
          }
      }
    }

    都是比较简单的逻辑,先调用依赖的start方法(如果依赖符合的话),然后再调用当前bean的start方法。

至此,Spring容器的初始化过程就算结束了,最后放一张全局的流程图(有点长):

关于BeanDefinition的载入与Bean的实例化(doGetBean),会有新的篇幅来详细解析,敬请期待~