springboot启动流程

//创建SpringApplication实例
public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
   this.resourceLoader = resourceLoader;
   Assert.notNull(primarySources, "PrimarySources must not be null");
   //将main启动类存入set
   this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
   //判断是否是web
   this.webApplicationType = WebApplicationType.deduceFromClasspath();
   //第一次通过SPI去读取spring.factories 创建并初始化ApplicationInitializers
   setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
   //第二次去读取spring.factories 创建初始化ApplicationListeners
   setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
   //初始化主类
   this.mainApplicationClass = deduceMainApplicationClass();
}
 //启动run方法

public ConfigurableApplicationContext run(String... args) {
   //创建stopwatch计时器,并启动
   StopWatch stopWatch = new StopWatch();
   stopWatch.start();
   ConfigurableApplicationContext context = null;
   Collection exceptionReporters = new ArrayList<>();
   configureHeadlessProperty();
   //获取SpringApplicationRunListeners
   SpringApplicationRunListeners listeners = getRunListeners(args);
   //启动ApplicationRunListeners
   listeners.starting();
   try {
      //创建ApplicationArguments
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
      //创建并实例化ConfigurableEnvironment
      ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
      configureIgnoreBeanInfo(environment);
      Banner printedBanner = printBanner(environment);
      //创建ConfigableApplicationContext
      context = createApplicationContext();
      exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
            new Class[] { ConfigurableApplicationContext.class }, context);
      //准备ConfigableApplicationContext
      prepareContext(context, environment, listeners, applicationArguments, printedBanner);
      //刷新ConfigableApplicationContext
      refreshContext(context);
      //容器刷新后动作
      afterRefresh(context, applicationArguments);
      //计时器停止
      stopWatch.stop();
      if (this.logStartupInfo) {
         new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
      }
      //发布ApplicationStartedEvent事件
      listeners.started(context);
      //调用ApplicationRunner,CommandLineRunner的run方法
      callRunners(context, applicationArguments);
   }
   catch (Throwable ex) {
      handleRunFailure(context, ex, exceptionReporters, listeners);
      throw new IllegalStateException(ex);
   }

   try {
      listeners.running(context);
   }
   catch (Throwable ex) {
      handleRunFailure(context, ex, exceptionReporters, null);
      throw new IllegalStateException(ex);
   }
   return context;
}

 

你可能感兴趣的:(springboot启动流程)