一、基本原理和实现逻辑

二、Spring 容器初始化流程源码阅读

Spirng 容器初始化主要包括一下几个主要部分:bean实例化过程、bean的创建流程、bean的依赖注入流程、bean的初始化过程。

2.1 主流程源码分析

  1. 源码入口

    1. //1. 创建IOC容器,并进行初始化
    2. ApplicationContext context = new ClassPathXmlApplicationContext("");
    3. //2. 获取创建的对象进行使用
    4. User user = context.getBean("beanName");
  2. ClassPathXmlApplicationContext 构造方法

    1. public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
    2. @Nullable
    3. private Resource[] configResources;
    4. // 根据String 类型的构造方法,读取bean.xml 配置文件进行后面的初始化操作
    5. public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
    6. // 调用本方法构造方法
    7. this(new String[] {configLocation}, true, null);
    8. }
    9. public ClassPathXmlApplicationContext(
    10. String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
    11. throws BeansException {
    12. // 实现父类构造方法,由于继承关系比较多,一层层实现最终到顶级父类
    13. super(parent);
    14. setConfigLocations(configLocations);
    15. if (refresh) {
    16. refresh();
    17. }
    18. }
    19. }
  3. AbstractApplicationContext

    1. public abstract class AbstractApplicationContext extends DefaultResourceLoader
    2. implements ConfigurableApplicationContext {
    3. @Override
    4. public void refresh() throws BeansException, IllegalStateException {
    5. synchronized (this.startupShutdownMonitor) {
    6. // Prepare this context for refreshing.
    7. // 1. 刷新预处理
    8. prepareRefresh();
    9. // Tell the subclass to refresh the internal bean factory.
    10. // 2:初始化流程
    11. 1)创建IOC容器(DefaultListableBeanFactory
    12. 2) 加载解析XML文件(最终存储到Document对象中)
    13. 3)读取Document对象,并完成BeanDefinition的加载和注册工作
    14. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    15. // Prepare the bean factory for use in this context.
    16. prepareBeanFactory(beanFactory);
    17. try {
    18. // Allows post-processing of the bean factory in context subclasses.
    19. postProcessBeanFactory(beanFactory);
    20. // Invoke factory processors registered as beans in the context.
    21. invokeBeanFactoryPostProcessors(beanFactory);
    22. // Register bean processors that intercept bean creation.
    23. registerBeanPostProcessors(beanFactory);
    24. // Initialize message source for this context.
    25. initMessageSource();
    26. // Initialize event multicaster for this context.
    27. initApplicationEventMulticaster();
    28. // Initialize other special beans in specific context subclasses.
    29. onRefresh();
    30. // Check for listener beans and register them.
    31. registerListeners();
    32. // Instantiate all remaining (non-lazy-init) singletons.
    33. //11 实例化剩余的单例bean(非懒加载方式)
    34. //注意事项: bean的IoC、DI和AOP都发在此步骤中
    35. finishBeanFactoryInitialization(beanFactory);
    36. // Last step: publish corresponding event.
    37. finishRefresh();
    38. }
    39. catch (BeansException ex) {
    40. if (logger.isWarnEnabled()) {
    41. logger.warn("Exception encountered during context initialization - " +
    42. "cancelling refresh attempt: " + ex);
    43. }
    44. // Destroy already created singletons to avoid dangling resources.
    45. destroyBeans();
    46. // Reset 'active' flag.
    47. cancelRefresh(ex);
    48. // Propagate exception to caller.
    49. throw ex;
    50. }
    51. finally {
    52. // Reset common introspection caches in Spring's core, since we
    53. // might not ever need metadata for singleton beans anymore...
    54. resetCommonCaches();
    55. }
    56. }
    57. }
    58. }

    2.2 创建BeanFactory流程源码分析

    2.3 加载BeanDifination 流程分析

    2.4 Bean 实例化流程分析