springboot启动逻辑分析-------简述SpringApplication.run

Posted itivy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot启动逻辑分析-------简述SpringApplication.run相关的知识,希望对你有一定的参考价值。

调用链路为:

    at org.springframework.boot.SpringApplication.run(SpringApplication.java:296)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at com.ivy.demo.DemoApplication.main(DemoApplication.java:16)

代码:

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
     //(1).记录开始时间 stopWatch.start(); ConfigurableApplicationContext context
= null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty();
     //(2).获取配置的所有的SpringApplicationRunListener实现类,并封装到SpringApplicationRunListeners类 SpringApplicationRunListeners listeners
= getRunListeners(args);
     //(3).调用listener列表starting方法 listeners.starting();
try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args);
       //(4).准备环境 ConfigurableEnvironment environment
= prepareEnvironment(listeners, applicationArguments);
       //(5).根据spring.beaninfo.ignore配置是否忽略属性 configureIgnoreBeanInfo(environment);
       //(6).打开欢迎字体 Banner printedBanner
= printBanner(environment);
       //(7).创建ApplicationContext context
= createApplicationContext();
       //(8).获取错误报告类 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; }

 

以上是关于springboot启动逻辑分析-------简述SpringApplication.run的主要内容,如果未能解决你的问题,请参考以下文章

Spring启动流程

【Spring】简述@Configuration配置类注册BeanDefinition到Spring容器的过程

源码分析|SpringBoot启动流程

SpringBoot简述springboot项目启动数据加载内存中的三种方法

Springboot启动过程

SpringBoot -- 项目结构+启动流程