所有初始化流程到包含在 (org.springframework.context.support) AbstractApplicationContext#refresh() 中:

    1. @Override
    2. public void refresh() throws BeansException, IllegalStateException {
    3. synchronized (this.startupShutdownMonitor) {
    4. StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
    5. // Step 1: 刷新预处理,不管之前有没有容器,现在都会清除掉重置信息
    6. // Prepare this context for refreshing.
    7. prepareRefresh();
    8. // Step 2: Spring 初始化 bean 工厂的流程
    9. // a) 创建 IoC 容器(DefaultListableBeanFactory)
    10. // b) 加载解析 XML 文件(最终存储到 Document 对象中)
    11. // c) 读取 Document 对象,并完成 BeanDefinition 的加载和注册工作
    12. // Tell the subclass to refresh the internal bean factory.
    13. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    14. // Step 3: 对 IoC 容器进行一些预处理(设置一些公共属性)
    15. // Prepare the bean factory for use in this context.
    16. prepareBeanFactory(beanFactory);
    17. try {
    18. // Step 4: 后置处理 bean 工厂,对 bean 工厂进行一些注册操作
    19. // Allows post-processing of the bean factory in context subclasses.
    20. postProcessBeanFactory(beanFactory);
    21. StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
    22. // Step 5: 调用 BeanFactoryPostProcessor 后置处理器对 BeanDefinition 处理,发生在 bean 创建之前
    23. // Invoke factory processors registered as beans in the context.
    24. invokeBeanFactoryPostProcessors(beanFactory);
    25. // Step 6: 注册 BeanPostProcessor 后置处理器
    26. // Register bean processors that intercept bean creation.
    27. registerBeanPostProcessors(beanFactory);
    28. beanPostProcess.end();
    29. // Step 7: 初始化一些消息源(比如处理国际化的 i18n 等消息源)
    30. // Initialize message source for this context.
    31. initMessageSource();
    32. // -------------------------------------- 基础容器中没有,只存在于高级容器中 -----------------------------------
    33. // Step 8:初始化应用事件广播器
    34. // Initialize event multicaster for this context.
    35. initApplicationEventMulticaster();
    36. // Step 9: 初始化一些特殊的 bean (内置 bean)
    37. // Initialize other special beans in specific context subclasses.
    38. onRefresh();
    39. // Step 10:注册一些监听器
    40. // Check for listener beans and register them.
    41. registerListeners();
    42. // -----------------------------------------------------------------------------------------------------
    43. // Step 11: 实例化剩余的单例 bean()非懒加载方式
    44. // 注意事项:Bean 的 IoC、DI 和 AOP 都是发生在此步骤
    45. // Instantiate all remaining (non-lazy-init) singletons.
    46. finishBeanFactoryInitialization(beanFactory);
    47. // Step 12: 完成刷新时,需要发布对外的事件
    48. // Last step: publish corresponding event.
    49. finishRefresh();
    50. }
    51. catch (BeansException ex) {
    52. if (logger.isWarnEnabled()) {
    53. logger.warn("Exception encountered during context initialization - " +
    54. "cancelling refresh attempt: " + ex);
    55. }
    56. // Destroy already created singletons to avoid dangling resources.
    57. destroyBeans();
    58. // Reset 'active' flag.
    59. cancelRefresh(ex);
    60. // Propagate exception to caller.
    61. throw ex;
    62. }
    63. finally {
    64. // Reset common introspection caches in Spring's core, since we
    65. // might not ever need metadata for singleton beans anymore...
    66. resetCommonCaches();
    67. contextRefresh.end();
    68. }
    69. }
    70. }

    总结下来,共有 12 步:

    • Step 1: 刷新预处理,不管之前有没有容器,现在都会清除掉重置信息
    • Step 2: Spring 初始化 bean 工厂的流程
    • Step 3: 对 IoC 容器进行一些预处理(设置一些公共属性)
    • Step 4: 后置处理 bean 工厂,对 bean 工厂进行一些注册操作
    • Step 5: 调用 BeanFactoryPostProcessor 后置处理器对 BeanDefinition 处理,发生在 bean 创建之前
    • Step 6: 注册 BeanPostProcessor 后置处理器
    • Step 7: 初始化一些消息源(比如处理国际化的 i18n 等消息源)
    • Step 8:初始化应用事件广播器
    • Step 9: 初始化一些特殊的 bean (内置 bean)
    • Step 10:注册一些监听器
    • Step 11: 实例化剩余的单例 bean()非懒加载方式
    • Step 12: 完成刷新时,需要发布对外的事件