SpringApplication
public ConfigurableApplicationContext run(String... args){// 计时器开始计时
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = 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;}
自定义启动器
方式一
public class FirstCommandlineRunner implements CommandLineRunner {
@Override
public voidrun(String... args) throws Exception {}}
order 值一样的时候 ApplicationRunner 优先于 CommandLineRunner 执行
方式二
public class FirstApplicationRunner implements ApplicationRunner {
@Override
public voidrun(ApplicationArguments args) throws Exception {}}
public ConfigurableApplicationContext run(String... args){// 计时器开始计时
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = 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;}