• spring源码-spring启动(待完善)


    spring启动代码

    1. // spring启动
    2. AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
    3. // 调AnnotationConfigApplicationContext构造方法
    4. public AnnotationConfigApplicationContext(Class... componentClasses) {
    5. // 构造DefaultListableBeanFactory、AnnotatedBeanDefinitionReader、ClassPathBeanDefinitionScanner
    6. this();
    7. register(componentClasses);
    8. refresh();
    9. }

    spring启动流程

    1. 构造方法, 创建DefaultListableBeanFactory(bean工厂),AnnotatedBeanDefinitionReader(读取器)和ClassPathBeanDefinitionScanner(扫描器)

    2. 解析配置类,注册BeanDefinition到BeanFactory, 扫描

    3. 准备阶段,添加一些类加载器, el表达式解析器, 类型转换器, 三个BeanPostProcessor, 以及ignoreDependencyInterface和ResolvableDependency

    4. 初始化国际化MessageSource对象

    5. 初始化事件发布器

    6. 把用户定义的ApplicationListener对象添加到ApplicationContext中,等Spring启动完就发布事件

    7. 实例化非懒加载单例bean, 存入单例池

    8. 调用Lifecycle Bean的start()方法

    9. 发布ContextRefreshedEvent事件

    refresh方法解析

    1. @Override
    2. public void refresh() throws BeansException, IllegalStateException {
    3. synchronized (this.startupShutdownMonitor) {
    4. StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
    5. // Prepare this context for refreshing.
    6. prepareRefresh();
    7. // Tell the subclass to refresh the internal bean factory.
    8. // 判断能否重复刷新,并且返回一个BeanFactory, 刷新不代表完全情况,主要是先执行Bean的销毁,然后重新生成一个BeanFactory,再在接下来的步骤中重新去扫描等等
    9. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    10. // Prepare the bean factory for use in this context.
    11. // 准备BeanFactory
    12. // 1. 设置BeanFactory的类加载器、SpringEL表达式解析器、类型转化注册器
    13. // 2. 添加三个BeanPostProcessor,注意是具体的BeanPostProcessor实例对象
    14. // 3. 记录ignoreDependencyInterface
    15. // 4. 记录ResolvableDependency
    16. // 5. 添加三个单例Bean
    17. prepareBeanFactory(beanFactory);
    18. try {
    19. // Allows post-processing of the bean factory in context subclasses.
    20. // 子类来设置一下BeanFactory
    21. postProcessBeanFactory(beanFactory);
    22. StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
    23. // Invoke factory processors registered as beans in the context.
    24. // BeanFactory准备好了之后,执行BeanFactoryPostProcessor,开始对BeanFactory进行处理
    25. // 默认情况下:
    26. // 此时beanFactory的beanDefinitionMap中有6个BeanDefinition,5个基础BeanDefinition+AppConfig的BeanDefinition
    27. // 而这6个中只有一个BeanFactoryPostProcessor:ConfigurationClassPostProcessor
    28. // 这里会执行ConfigurationClassPostProcessor进行@Component的扫描,扫描得到BeanDefinition,并注册到beanFactory中
    29. // 注意:扫描的过程中可能又会扫描出其他的BeanFactoryPostProcessor,那么这些BeanFactoryPostProcessor也得在这一步执行
    30. invokeBeanFactoryPostProcessors(beanFactory); // scanner.scan()
    31. // Register bean processors that intercept bean creation.
    32. // 将扫描到的BeanPostProcessors实例化并排序,并添加到BeanFactory的beanPostProcessors属性中去
    33. registerBeanPostProcessors(beanFactory);
    34. beanPostProcess.end();
    35. // Initialize message source for this context.
    36. // 设置ApplicationContext的MessageSource,要么是用户设置的,要么是DelegatingMessageSource
    37. initMessageSource();
    38. // Initialize event multicaster for this context.
    39. // 设置ApplicationContext的applicationEventMulticaster,要么是用户设置的,要么是SimpleApplicationEventMulticaster
    40. initApplicationEventMulticaster();
    41. // Initialize other special beans in specific context subclasses.
    42. // 给子类的模板方法
    43. onRefresh();
    44. // Check for listener beans and register them.
    45. // 把定义的ApplicationListener的Bean对象,设置到ApplicationContext中去,并执行在此之前所发布的事件
    46. registerListeners();
    47. // Instantiate all remaining (non-lazy-init) singletons.
    48. finishBeanFactoryInitialization(beanFactory);
    49. // Last step: publish corresponding event.
    50. finishRefresh();
    51. }
    52. catch (BeansException ex) {
    53. if (logger.isWarnEnabled()) {
    54. logger.warn("Exception encountered during context initialization - " +
    55. "cancelling refresh attempt: " + ex);
    56. }
    57. // Destroy already created singletons to avoid dangling resources.
    58. destroyBeans();
    59. // Reset 'active' flag.
    60. cancelRefresh(ex);
    61. // Propagate exception to caller.
    62. throw ex;
    63. }
    64. finally {
    65. // Reset common introspection caches in Spring's core, since we
    66. // might not ever need metadata for singleton beans anymore...
    67. resetCommonCaches();
    68. contextRefresh.end();
    69. }
    70. }
    71. }
    1. prepareRefresh()
      1. 记录启动时间
      2. 可以允许子容器设置一些内容到Environment中
      3. 验证Environment中是否包括了必须要有的属性
    2. obtainFreshBeanFactory()

      1. 判断能否重复刷新

      2. 调子类的refreshBeanFactory方法获取bean

    3. prepareBeanFactory(beanFactory)

      1. 设置beanFactory的类加载器
      2. 设置表达式解析器:StandardBeanExpressionResolver,用来解析Spring中的表达式
      3. 添加PropertyEditorRegistrar:ResourceEditorRegistrar,PropertyEditor类型转化器注册器,用来注册一些默认的PropertyEditor
      4. 添加一个Bean的后置处理器:ApplicationContextAwareProcessor,是一个BeanPostProcessor,用来执行EnvironmentAware、ApplicationEventPublisherAware等回调方法
      5. 添加ignoredDependencyInterface:可以向这个属性中添加一些接口,如果某个类实现了这个接口,并且这个类中的某些set方法在接口中也存在,那么这个set方法在自动注入的时候是不会执行。比如EnvironmentAware这个接口,如果某个类实现了这个接口,那么就必须实现它的setEnvironment方法,而这是一个set方法,和Spring中的autowire是冲突的,那么Spring在自动注入时是不会调用setEnvironment方法的,而是等到回调Aware接口时再来调用(注意,这个功能仅限于xml的autowire,@Autowired注解是忽略这个属性的)
    4. postProcessBeanFactory(beanFactory) : 提供给AbstractApplicationContext的子类进行扩展
    5. invokeBeanFactoryPostProcessors:执行BeanFactoryPostProcessor

      1. 解析AppConfig类
      2. 扫描得到BeanDefinition并注册
      3. 解析@Import,@Bean等注解得到BeanDefinition并注册
    6. registerBeanPostProcessors:把BeanFactory中所有的BeanPostProcessor找出来并实例化得到一个对象,并添加到BeanFactory中去

    7. initMessageSource:如果BeanFactory中存在一个叫messageSource的BeanDefinition,那么会把这个Bean对象创建出来并赋值给ApplicationContext的messageSource属性,让ApplicationContext拥有国际化的功能

    8. initApplicationEventMulticaster: 如果BeanFactory中存在一个叫做"applicationEventMulticaster"的BeanDefinition,那么会把这个Bean对象创建出来并赋值给ApplicationContext的applicationEventMulticaster属性,让ApplicationContext拥有事件发布的功能

    9. onRefresh:子类模板方法, 扩展点

    10. registerListeners:把定义的ApplicationListener的Bean对象,设置到ApplicationContext中去,并执行在此之前所发布的事件

    11. finishBeanFactoryInitialization:完成BeanFactory的初始化,实例化非懒加载的单例Bean

    12. finishRefresh

      1. 设置lifecycleProcessor

      2. 调用LifecycleBean的start

      3. 发布ContextRefreshedEvent事件

     

  • 相关阅读:
    react-antd项目,一个多tab页面,共用一个title相同的table表格,并且在切换tab时实现数据更新
    Linux——vim简介、配置方案(附带超美观的配置方案)、常用模式的基本操作
    YOLO性能指标
    运营攻略︱推动用户增长的方法
    上门预约按摩家政小程序开发;
    一个最简verilog代码的分析
    Zabbix在X86服务器上的部署流程
    git新建仓库提交项目代码+常用命令
    9.SpringBoot与调度器
    部署在阿里云ECS服务器上的微服务项目中获取到的时间和windows的时间不一样的问题
  • 原文地址:https://blog.csdn.net/weixin_64027360/article/details/134410515