Spring Boot是如何启动的呢?

璐璐 Java面经 发布时间:2022-11-05 11:51:38 阅读数:8317 1
下文笔者讲述SpringBoot的启动流程,如下所示
由于SpringBoot的入口函数为main方法
  所以我们需从man方法中分析具体的启动代码,如下所示
 
@SpringBootApplication
public class Application {
   public static void main(String[] args) throws Exception {
       SpringApplication.run(Application.class,args);
  }
}

SpringBoot启动类源码分析

Application类上加入:
   @SpringBootApplication注解
   SpringApplication.run方法中传入
     Application.class和相应的参数信息

一、@SpringBootApplication注解说明
   @SpringBootApplication注解=@SpringBootConfiguration+@ComponentScan+@EnableAutoConfiguration  

@SpringBootConfiguration:与@Bean一起完成JavaConfig配置
@ComponentScan:通过范围扫描的方式,扫描特定注解注释的类,将其注册到Spring容器
@EnableAutoConfiguration:通过spring.factories配置
      并结合@Condition条件,完成bean注册

注意事项:
    我们还可使用@Import注解导入class到Spring容器中

Spring Boot 启动流程

  
SpringApplication的实例化

检测应用类型是否是Web环境

设置初始化器(Initializer)

设置监听器(listener)

推断应用入口类(Main)

SpringApplication.run方法

获取SpringApplicationRunListeners

准备配置环境ConfigurableEnvironment

创建ApplicationContext应用上下文

ApplicationContext前置处理

ApplicationContext刷新

ApplicationContext后置处理

完成了实例化,下面开始调用run方法

// 运行run方法
public ConfigurableApplicationContext run(String... args) {
   // 此类通常用于监控开发过程中的性能,而不是生产应用程序的一部分。
   StopWatch stopWatch = new StopWatch();
   stopWatch.start();

   ConfigurableApplicationContext context = null;
   Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();

   // 设置java.awt.headless系统属性,默认为true
   // Headless模式是系统的一种配置模式。在该模式下,系统缺少了显示设备、键盘或鼠标。
   configureHeadlessProperty();

   // KEY 1 - 获取SpringApplicationRunListeners
   SpringApplicationRunListeners listeners = getRunListeners(args);

   // 通知监听者,开始启动
   listeners.starting();
   try {
       ApplicationArguments applicationArguments = new DefaultApplicationArguments(
               args);

       // KEY 2 - 根据SpringApplicationRunListeners以及参数来准备环境
       ConfigurableEnvironment environment = prepareEnvironment(listeners,
               applicationArguments);
       
       configureIgnoreBeanInfo(environment);

       // 准备Banner打印器 - 就是启动Spring Boot的时候打印在console上的ASCII艺术字体
       Banner printedBanner = printBanner(environment);

       // KEY 3 - 创建Spring上下文
       context = createApplicationContext();

       // 注册异常分析器
       analyzers = new FailureAnalyzers(context);

       // KEY 4 - Spring上下文前置处理
       prepareContext(context, environment, listeners, applicationArguments,
               printedBanner);

       // KEY 5 - Spring上下文刷新
       refreshContext(context);

       // KEY 6 - Spring上下文后置处理
       afterRefresh(context, applicationArguments);

       // 发出结束执行的事件
       listeners.finished(context, null);

       stopWatch.stop();
       if (this.logStartupInfo) {
           new StartupInfoLogger(this.mainApplicationClass)
                  .logStarted(getApplicationLog(), stopWatch);
      }
       return context;
  }
   catch (Throwable ex) {
       handleRunFailure(context, listeners, exceptionReporters, ex);
       throw new IllegalStateException(ex);
  }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaMianJing/202211/16676203404798.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者