Spring Boot启动流程断点过程解析

前端之家收集整理的这篇文章主要介绍了Spring Boot启动流程断点过程解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这篇文章主要介绍了Spring Boot启动流程断点过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

启动入口

Spring Boot启动流程断点过程解析


跟进run方法 : 一个用来使用默认的配置从特定的源运行SpringApplication的静态帮助类。

这个类有两个重载方法,另一个用来传入多个源。通常,单个参数方法是数组方法的一个特例

Spring Boot启动流程断点过程解析


创建一个新的SpringApplication实例。这个应用程序上下文会从特定的源加载Beans,这个实例会在调用run方法之前被定制化。

Web应用程序类型的枚举:WebApplicationType,包含NONE(不是web应用),SERVLET(基于Servlet的web应用),REACTIVE(基于Reactive的web应用)

  • 直接jar包运行不使用web容器
  • 使用嵌入式的Servlet web容器
  • 使用反应式的web容器

Spring Boot启动流程断点过程解析


  1. setInitializers((Collection) getSpringFactoriesInstances(
  2. ApplicationContextInitializer.class));

用于创建和加载Spring工厂方法实例

Spring Boot启动流程断点过程解析


Spring Boot启动流程断点过程解析


Spring Boot启动流程断点过程解析


Spring Boot启动流程断点过程解析


4.运行SpringApplication的run方法

Spring Boot启动流程断点过程解析


Java SPI在 Spring Boot中的应用

SpringBoot底层的自动化都是由这些SPI实现类来实现的:初始化,监听器,自动配置导入监听器,自动配置导入过滤器,自动配置,失败分析器,可用模板提供者

Spring Boot启动流程断点过程解析


Spring Boot找到main方式的方式

通过抛异常的形式来获取堆栈信息,再获取启动类的信息。

Spring Boot启动流程断点过程解析


以上都是new SpringBootApplication的过程,下面分析run方法

  1. /**
  2. * Run the Spring application,creating and refreshing a new
  3. * {@link ApplicationContext}.
  4. * 运行一个Spring应用,创建和刷新一个新的ApplicationContext
  5. * @param args the application arguments (usually passed from a Java main method)
  6. * 应用参数通过java main方法传递过来
  7. * @return a running {@link ApplicationContext}
  8. */
  9. public ConfigurableApplicationContext run(String... args) {
  10. // 任务计时器工具,可同时计数多个任务
  11. StopWatch stopWatch = new StopWatch();
  12. stopWatch.start();
  13. //ApplicationContext是Spring的中心接口,为应用提供配置:1bean工厂2加载资源3注册的监听器发布事件4解析消息
  14. ConfigurableApplicationContext context = null;
  15. Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
  16. //headless 模式:服务器端模式,表示系统没有键盘鼠标等前端应用
  17. configureHeadlessProperty();
  18. //监听器容器,对run方法各个阶段事件进行监听,观察者模式
  19. SpringApplicationRunListeners listeners = getRunListeners(args);
  20. //监听相应的事件,SpringApplicationEvent下的一个实现
  21. listeners.starting();
  22. try {
  23. //提供了对于运行SpringApplication参数的访问
  24. ApplicationArguments applicationArguments = new DefaultApplicationArguments(
  25. args);
  26. //环境配置:是servlet,reactive或者java应用环境,触发evn准备好的事件
  27. ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
  28. configureIgnoreBeanInfo(environment);
  29. Banner printedBanner = printBanner(environment);
  30. context = createApplicationContext();
  31. exceptionReporters = getSpringFactoriesInstances(
  32. SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class },context);
  33. prepareContext(context,environment,listeners,applicationArguments,printedBanner);
  34. refreshContext(context);
  35. afterRefresh(context,applicationArguments);
  36. stopWatch.stop();
  37. if (this.logStartupInfo) {
  38. new StartupInfoLogger(this.mainApplicationClass)
  39. .logStarted(getApplicationLog(),stopWatch);
  40. }
  41. listeners.started(context);
  42. callRunners(context,applicationArguments);
  43. }
  44. catch (Throwable ex) {
  45. handleRunFailure(context,ex,exceptionReporters,listeners);
  46. throw new IllegalStateException(ex);
  47. }
  48.  
  49. try {
  50. listeners.running(context);
  51. }
  52. catch (Throwable ex) {
  53. handleRunFailure(context,null);
  54. throw new IllegalStateException(ex);
  55. }
  56. return context;
  57. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的Java相关文章