一、一行完成启动
@SpringBootApplication public class Sb2Application { public static void main(String[] args) { SpringApplication.run(Sb2Application.class, args); } }
进入SpringApplication的run方法
SpringApplication.run(Sb2Application.class, args);
分为两步: 一步是初始化SpringApplication, new SpringApplication,一步是调用SpringApplication的run方法
第一步:初始化SpringApplication
public SpringApplication(Class... primarySources) { this((ResourceLoader)null, primarySources); } public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) { this.sources = new LinkedHashSet(); this.bannerMode = Mode.CONSOLE; this.logStartupInfo = true; this.addCommandLineProperties = true; this.addConversionService = true; this.headless = true; this.registerShutdownHook = true; this.additionalProfiles = new HashSet(); this.isCustomEnvironment = false; this.lazyInitialization = false; this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); this.primarySources = new LinkedHashSet(Arrays.asList(primarySources)); this.webApplicationType = WebApplicationType.deduceFromClasspath(); this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = this.deduceMainApplicationClass(); }
第二步 调用SpringApplication的run方法
/** * Run the Spring application, creating and refreshing a new * {@link ApplicationContext}. * @param args the application arguments (usually passed from a Java main method) * @return a running {@link ApplicationContext} */ public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; CollectionexceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); } listeners.started(context); 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; }
二、启动流程
1、框架初始化
2、框架启动
3、自动化装配
1、框架初始化步骤
框架初始化步骤:
配置资源加载器
配置primarySources
应用环境检测
配置系统初始化器
配置应用监听器
配置main方法所在的类
2、框架启动
计时器开始计时 -> Headless模式赋值 -> 发送ApplicationStartEvent
-> 配置环境模块 -> 发送ApplicationEnvironmentPreparedEvent
-> 打印banner(打印出来的Spring 1.X 或者Spring 2.X条幅)
-> 创建应用上下文对象 -> 初始化失败分析器
-> 关联springboot组件与应用上下文对象 -> 发送ApplicationContextInitializedEvent
-> 加载source到context -> 发送ApplicationPreparedEvent
-> 刷新上下文(调用Spring框架的DoRefresh方法)
->计时器停止计时 -> 发送ApplicationStartedEvent
-> 调用框架启动扩展类 -> 发送ApplicationReadyEvent
3、自动化装配
收集配置文件中的配置工厂类
加载组件工厂
注册组件内定义bean
Spring Boot的完整流程图