流程图

Spring源码总结.xmind


核心接口

图片.png

Spring 声明周期

image.png

Bean 加载流程

Spring 流程图 - 图3
执行流程详解

  1. 加载 Bean 定义信息(xml 配置、注解、配置类);
  2. 解析 Bean 信息(BeanDefinitionReader 读取器 Bean 信息);
  3. 读取 Bean 定义信息,用 ConcurrentHashMap 存储(BeanDefinition 存放Bean 实例、作用域等);
  4. BeanFactoryPostProcessor 扩展接口(BeanDefinition 属性增强器,通过 PlaceholderConfigurerSupport解析占位符, 如 @Value(“${xxxx}”)替换);
  5. 通过反射来创建 Bean 对象信息;
  6. 初始化对象(在堆中开辟新的空间→属性都是默认值);
  7. 如果该方法依赖其它的接口(@Autowired),通过 DI 注入(填充属性 populateBean 方法);
  8. *aware接口实现(设置aware接口属性:BeanNameAware等等);
  9. 如果实现了 BeanPostProcessor 接口(增强器,可以对方法前置和后置进行扩展);
  10. 前置增强器(postProcessBeforeInitialization 方法);
  11. Bean 初始化[init-method];
  12. 后置增强器(postProcessAfterInitialization 方法);
  13. Bean基本使用;
  14. 销毁(DisposableBean);

获取 bean 调用流程方法

image.png

Bean完成对象初始化过程

  1. org.springframework.context.annotation.AnnotationConfigApplicationContext
  2. org.springframework.context.support.AbstractApplicationContext#refresh
  3. org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization
  4. org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons
  5. org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String)--→849
  6. org.springframework.beans.factory.support.AbstractBeanFactory#createBean--→320
  7. --------------------------------------------------------------------------------------
  8. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean--→515
  9. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance--→555
  10. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean--→592
  11. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean--→593

Bean生命周期处理器

  1. 1. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInstantiation 1144
  2. 2. org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor#determineCandidateConstructors 1324
  3. 3. MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition 1094
  4. 4. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#getEarlyBeanReference 962
  5. 5. InstantiationAwareBeanPostProcessor 1452
  6. 6. org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor#postProcessProperties 1502
  7. 7. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization 1914
  8. 8. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization 1940

Bean 创建对象有几种方法

  1. new
  2. 反射