public ConfigurableApplicationContext run(String... args) {
任務執行時長spring
StopWatch stopWatch = new StopWatch(); app
stopWatch.start();less
Spring 上下文:ConfigurableApplicationContext
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
this
Headless模式是在缺乏顯示屏、鍵盤或者鼠標是的系統配置。
Headless模式是系統的一種配置模式。在系統可能缺乏顯示設備、鍵盤或鼠標這些外設的狀況下可使用該模式。
spa
configureHeadlessProperty();
實例化監聽spring的監聽器,EventPublishingRunListener,根據配置文件,日誌
以及過濾器SpringApplicationRunListener,實例化監聽對象,經過class.forName.code
SpringApplicationRunListeners listeners = getRunListeners(args);
啓動事件監聽對象
listeners.starting();blog
try {
構造容器參數信息接口
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
準備環境變量,初始爲StandardServletEnvironment
標準的servlet環境。給上邊的監聽對象,綁定環境變量信息 具體參考裏邊的方法。
ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
經過配置忽略Bean的信息
configureIgnoreBeanInfo(environment);
打印 banner日誌。
Banner printedBanner = printBanner(environment);
建立springApplication上下文。根據servlet類型:實例的對象是:
AnnotationConfigServletWebServerApplicationContext
context = createApplicationContext();
從配置文件中加載,加載SpringBootExceptionReporter的子類。
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context);
準備上下文信息。
prepareContext(context, environment, listeners, applicationArguments,printedBanner);
刷新容器信息,採用的是spring的加載過程。
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
// 監聽執行started方法
listeners.started(context);、
// 執行CommandLineRunner 和ApplicationRunner的方法
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;
}
主要完成boot啓動的時,執行的本身得一套邏輯代碼,能夠根據資源,容器的初始話過程,執行不一樣的邏輯代碼信息
public interface SpringApplicationRunListener {
void starting();
/**
* Called once the environment has been prepared, but before the
* {@link ApplicationContext} has been created.
* @param environment the environment
*/
void environmentPrepared(ConfigurableEnvironment environment);
/**
* Called once the {@link ApplicationContext} has been created and prepared, but
* before sources have been loaded.
* @param context the application context
*/
void contextPrepared(ConfigurableApplicationContext context);
/**
* Called once the application context has been loaded but before it has been
* refreshed.
* @param context the application context
*/
void contextLoaded(ConfigurableApplicationContext context);
/**
* The context has been refreshed and the application has started but
* {@link CommandLineRunner CommandLineRunners} and {@link ApplicationRunner
* ApplicationRunners} have not been called.
* @param context the application context.
* @since 2.0.0
*/
void started(ConfigurableApplicationContext context);
/**
* Called immediately before the run method finishes, when the application context has
* been refreshed and all {@link CommandLineRunner CommandLineRunners} and
* {@link ApplicationRunner ApplicationRunners} have been called.
* @param context the application context.
* @since 2.0.0
*/
void running(ConfigurableApplicationContext context);
/**
* Called when a failure occurs when running the application.
* @param context the application context or {@code null} if a failure occurred before
* the context was created
* @param exception the failure
* @since 2.0.0
*/
void failed(ConfigurableApplicationContext context, Throwable exception);
}