SpringBoot启动流程
------------恢复内容开始------------
一、首先会创建SpringApplication实例,注释部分功能和没注释功能一样,其实底层都是先创建SpringApplciation对象,在调用run方法。
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class SpringBootTestApplication { public static void main(String[] args) { SpringApplication.run(SpringBootTestApplication.class, args); // SpringApplication springApplication = new SpringApplication(SpringBootTestApplication.class); //springApplication.run(args); } }
二、SpringApplication创建过程
@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;// resourceLoader默认传递null
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();//主要判断当前服务是什么类型servlet /reactive
/*主要初始化ApplicationContextInitializer接口的实例,
*(1)可以基于SPJ,在META-INF/spring.factories填写自定义ApplicationContextInitializer实例
*(2)可以ConfigableApplicationContext上下文中添加addInitilaizer();
*(3)可以在配置文件中添加application.properties
*ApplicationContextInitializer功能主要是:
*用于在spring容器刷新之前初始化Spring ConfigurableApplicationContext的回调接口。
* 通常用于需要对应用程序上下文进行编程初始化的web应用程序中。例如,根据上下文环境注册属性源或激活配置文件等。
*/
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
//主要初始化ApplicationListener接口的实例,可以基于SPJ,在META-INF/spring.factories填写自定义ApplicationListener实例
/*
* 创建过程同上
* 功能主要就是监听器,可以自定义监听器监听某个事件
*/
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();//从多个配置类中找到有main方法的主配置类
}
三、run()方法执行过程
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch();//记录时间的工具类 stopWatch.start(); ConfigurableApplicationContext context = null;//应用上下文定义 //异常处理类集合,下面会从META-INF/spring.factories文件读取 Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty();//主要给java.awt.headless属性赋值,保证System.getProperty();一定能取到该值 /* * 接口的作用主要就是在Spring Boot 启动初始化的过程中可以通过SpringApplicationRunListener接口回调来让用户在启动的各个流程中可以加入自己的逻辑 * 当然内部也是从META-INF/spring.factories文件读取,也会调用getSpringFactoriesInstances()这个方法 */ SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting();//启动实现starting方法的所有SpringApplicationRunListeners实例 try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//初始化参数 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);//初始化运行环境(后面详解) configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); //读取SpringBootExceptionReporter实例 exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); //准备应用上下文,里面会调用ApplicationContextInitializer的initializer方法一些参数初始化,(后面详解) prepareContext(context, environment, listeners, applicationArguments, printedBanner); //刷新应用上下文,里面会加载bean的实例,本文不详解(后面单独详解) refreshContext(context); //是个空方法,里给用户扩展 afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } //启动实现started方法的所有SpringApplicationRunListeners实例 listeners.started(context); //ApplicationRunner、CommandLineRunner扩展类,启动 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; }
------------恢复内容结束------------

浙公网安备 33010602011771号