springboot启动流程

    技术2024-12-26  15

    第一步

    //构建启动环境 public ConfigurableApplicationContext run(String... args) { //开始启动 StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); //设置主要的启动参数 configureHeadlessProperty(); //获取 SpringApplicationRunListeners SpringApplicationRunListeners listeners = getRunListeners(args); //执行SpringApplicationRunListeners 的starting listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); //打印banner Banner printedBanner = printBanner(environment); //创建spring上下文(web环境) context = createApplicationContext(); //ioc容器初始化(重要) 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); } //SpringApplicationRunListener执行started listeners.started(context); //执行ApplicationRunner和CommandLineRunner 的run方法 有顺序 callRunners(context, applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { //执行SpringApplicationRunListener running listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } //返回环境变量 return context; }
    Processed: 0.010, SQL: 9