Spring Boot启动流程断点过程解析
创建一个新的SpringApplication实例。这个应用程序上下文会从特定的源加载Beans,这个实例会在调用run方法之前被定制化。
- 直接jar包运行不使用web容器
- 使用嵌入式的Servletweb容器
- 使用反应式的web容器
setInitializers((Collection)getSpringFactoriesInstances( ApplicationContextInitializer.class));
用于创建和加载Spring工厂方法实例
4.运行SpringApplication的run方法
JavaSPI在SpringBoot中的应用
SpringBoot找到main方式的方式
以上都是newSpringBootApplication的过程,下面分析run方法
/** *RuntheSpringapplication,creatingandrefreshinganew *{@linkApplicationContext}. *运行一个Spring应用,创建和刷新一个新的ApplicationContext *@paramargstheapplicationarguments(usuallypassedfromaJavamainmethod) *应用参数通过javamain方法传递过来 *@returnarunning{@linkApplicationContext} */ publicConfigurableApplicationContextrun(String...args){ //任务计时器工具,可同时计数多个任务 StopWatchstopWatch=newStopWatch(); stopWatch.start(); //ApplicationContext是Spring的中心接口,为应用提供配置:1bean工厂2加载资源3注册的监听器发布事件4解析消息 ConfigurableApplicationContextcontext=null; CollectionexceptionReporters=newArrayList<>(); //headless模式:服务器端模式,表示系统没有键盘鼠标等前端应用 configureHeadlessProperty(); //监听器容器,对run方法各个阶段事件进行监听,观察者模式 SpringApplicationRunListenerslisteners=getRunListeners(args); //监听相应的事件,SpringApplicationEvent下的一个实现 listeners.starting(); try{ //提供了对于运行SpringApplication参数的访问 ApplicationArgumentsapplicationArguments=newDefaultApplicationArguments( args); //环境配置:是servlet,reactive或者java应用环境,触发evn准备好的事件 ConfigurableEnvironmentenvironment=prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); BannerprintedBanner=printBanner(environment); context=createApplicationContext(); exceptionReporters=getSpringFactoriesInstances( SpringBootExceptionReporter.class, newClass[]{ConfigurableApplicationContext.class},context); prepareContext(context,environment,listeners,applicationArguments, printedBanner); refreshContext(context); afterRefresh(context,applicationArguments); stopWatch.stop(); if(this.logStartupInfo){ newStartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(),stopWatch); } listeners.started(context); callRunners(context,applicationArguments); } catch(Throwableex){ handleRunFailure(context,ex,exceptionReporters,listeners); thrownewIllegalStateException(ex); } try{ listeners.running(context); } catch(Throwableex){ handleRunFailure(context,ex,exceptionReporters,null); thrownewIllegalStateException(ex); } returncontext; }