springboot启动流程
2020-05-28 14:45:04   0  举报             
     
         
 SpringBoot启动流程图
    作者其他创作
 大纲/内容
 getMergedLocalBeanDefinition
  SpringFactoriesLoader.loadFactoryNames()
  refresh()->synchronized (this.startupShutdownMonitor)
    supportsEvent()
  this.beanFactory.setSerializationId(getId()) 
  prepareContext()
  步骤四
  是
  否
  printBanner(environment)
  cache==null
  注册一些组件
  getSpringFactoriesInstances()
  EVENT_TYPES:当前监听器感兴趣的事件类型
  DelegatingApplicationContextInitializer(支线任务)
  getSpringFactoriesInstances
  if (!this.banner.isEmpty())
  步骤一
  return banner
  广播器触发机制
  invokeBeanFactoryPostProcessors
  获取bean工厂
  添加至 currentRegistryProcessors集合中
  循环遍历步骤三直至beanFactory中不存在未处理的BeanDefinitionRegistryPostProcessor实现
  refreshBeanFactory() 
  将上述结果添加到 registryProcessors集合中
  实例化bean
  createSpringFactoriesInstances
  this.fallbackBanner != null
  getInitializerClasses()
  SOURCE_TYPES:当前监听器感兴趣的源类型
  遍历beanFactoryPostProcessors
  onRefresh() 
  注册一些解析依赖
  设置系统监听器
  registerListeners() 
  SpringApplicationRunListener
  遍历
  return DEFAULT_BANNER
  createBeanInstance 
  initialize()
  由子类实现
  判断当前监听器是否是GenericApplicationListener类型
  实现了BeanDefinitionRegistryPostProcessor
  添加后置处理器
  prepareRefresh()
  location = spring.banner.image.locationIMAGE_EXTENSION = { \"gif\
  TIPS:1. 实现ApplicationListener接口针对单一事件监听;2. 实现SmartApplicationListener接口针对多种事件监听3. Order值越小越先执行4. application.properties中定义的执行顺序优于其他方式(详解见DelegatingApplicationListener。原理同DelegatingApplicationContextInitializer)
  对集合currentRegistryProcessors排序
  obtainFreshBeanFactory() 
  SpringApplication.run()
  initializeBean
  清空上述方法中产生的缓存
  initApplicationEventMulticaster()
  初始化事件广播器
  派发早期事件
  doGetBean
  instantiate
  AnnotationAwareOrderComparator.sort()根据Order注解排序
  初始化bean
  PropertiesLoaderUtils.loadProperties
  cache.put()
  retrieveApplicationListeners()
  BeanUtils.instantiateClass()创建实例
  currentRegistryProcessors集合内依次调用postProcessBeanDefinitionRegistry方法
  1.调用BeanDefinitionRegistryPostProcessor实现向容器内添加bean的定义2.调用BeanFactoryPostProcessors实现向容器内bean的定义添加属性
  读取jar包下META-INF/spring.factories文件
  instantiateInitializer()
  判断当前监听器对当前事件类型是否感兴趣
  &&
  createBean
  EventPublishingRunListener
  LoggingApplicationListener
  initMessageSource()
  initPropertySources()
  createApplicationContext()
  multicastEvent()
  resetCommonCaches()
  registerBeanPostProcessors(beanFactory) 
  子类重写,以在BeanFactory完成创建后做进一步设置
  applyInitializers()
  向广播器注册监听器的实现
  设置序列化ID
  步骤二、三
  finishBeanFactoryInitialization(beanFactory) 
  1.找到BeanPostProcessor的实现2.排序并注册到容器中(注册过的则会移除再添加)
  supportsEventType
  applyInitializerClasses()
  getTextBanner
  设置忽略的自动装配接口
  bannerPrinter.print()
  preInstantiateSingletons
  与SpringApplication类中创建实例一致
  setListeners(getSpringFactoriesInstances(ApplicationListener.class))
  初始化属性设置
  getBean
  beanFactory.registerSingleton
  run()
  beanFactory.setBeanClassLoader
  instantiateBean
  populateBean
  listener.onApplicationEvent(event)
  true
  调用postProcessBeanDefinitionRegistry方法
  DEFAULT_BANNER = new SpringBootBanner();
  与上接口类似(上接口是获取BeanDefinitionRegistryPostProcessor)1.获取BeanPostProcessor;2.排序;3.注册到容器当中不同点:在于不会调用bean的postProcessBeanFactory实现方法
  callRunners()
  this.active.set(true)
  postProcessBeanFactory(beanFactory) 
  设置Bean加载器
  空方法,由子类实现web环境中,则构建web容器(tomcat、jetty等)
  初始化所有剩下的单例bean
  容器状态设置
  getApplicationEventMulticaster().multicastEvent(earlyEvent)
  添加至processedBeans集合中
  遍历initializer调用initialize()
  将spring.factories文件中的配置按K,V形式读取,多个初始化器用\
  开始
  getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName)
  invokeBeanFactoryPostProcessors(beanFactory)
  getBeanFactory() 
  registryProcessors集合依次调用postProcessBeanFactory方法
  前期准备
  cache.get()
  getSingleton
  准备bean工厂
  beanFactory.registerResolvableDependency
  包括依赖jar中和当前项目中spring.factories文件
  finishRefresh()
  判断当前beanFactory中是否含有广播器的实现,没有则new SimpleApplicationEventMulticaster
  setInitializers(getSpringFactoriesInstances(ApplicationContextInitializer.class))
  implements
  添加至registryProcessors集合中
  getImageBanner
  检查必备属性是否存在
  prepareBeanFactory(beanFactory) 
  遍历集合依次调用postProcessBeanFactory方法
  读取环境配置文件中参数为context.initializer.classes的配置,多值用\
  初始化国际化相关属性
  返回DefaultListableBeanFactory工厂类
  封装成GenericApplicationListener类型new GenericApplicationListenerAdapter(listener)
  支线任务:因为这里也会实例化环境配置文件(application.yml)中的初始化器类并调用其初始化方法该类order默认为0
  Ordered
  遍历BeanDefinitionRegistryPostProcessor实现
  ....Initializer
  获取监听器列表
  beanFactory.ignoreDependencyInterface 
  实现了PriorityOrdered接口
  doCreateBean
  getEnvironment().validateRequiredProperties()
  beanFactory.addBeanPostProcessor 
  添加至regularPostProcessors集合中
  supportsSourceType
  allListeners.add(listener)
  location=spring.banner.locationor banner.txt
  设置系统初始化器
  getRunListeners(args) -> getSpringFactoriesInstances(SpringApplicationRunListeners.class)
  getBanner(environment) 
  regularPostProcessors集合依次调用postProcessBeanFactory方法
  new SpringApplication().run()
  清空currentRegistryProcessors集合
  具体监听器的supportsEventType()方法和supportsSourceType()方法
  getBeanFactoryPostProcessors()
  1.清除缓存2.初始化生命周期处理器3.调用生命周期处理器的onRefresh()方法4.发布ContextRefreshedEvent事件5.JMX相关处理
  refreshContext()
  listeners.starting()
   
 
 
 
 
  0 条评论
 下一页
  
   
   
   
   
  
  
  
  
  
  
  
  
 