spring boot 入口源碼分析

public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch(); //時間計數器,主要記錄任務的運行時間 不用管
stopWatch.start();
ConfigurableApplicationContext context = null; //初始化一個可配置應用上下文
FailureAnalyzers analyzers = null; //失敗分析器
configureHeadlessProperty(); //設置系統 awt的頭屬性信息
SpringApplicationRunListeners listeners = getRunListeners(args); //設置多個運行監聽器
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args); //設置應用參數
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments); //得到創建的環境變量,設置監聽器的環境
Banner printedBanner = printBanner(environment); //設置banner,以及banner的資源加載器
context = createApplicationContext(); //創建應用上下文,可配置的web應用上下文或者標準的應用上下文
analyzers = new FailureAnalyzers(context); //設置失敗分析器 並且判斷是否是beanFactoryAware 如果是 將上下文的beanFactory賦值給它的beanFactory
prepareContext(context, environment, listeners, applicationArguments,
printedBanner); //設置上下文的環境 監聽器 應用參數,橫幅
refreshContext(context);
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
return context;
}
catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex);
throw new IllegalStateException(ex);
}
}




private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
context.setEnvironment(environment);
postProcessApplicationContext(context);
applyInitializers(context);
listeners.contextPrepared(context);
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}

// Add boot specific singleton beans
context.getBeanFactory().registerSingleton("springApplicationArguments",
applicationArguments);
if (printedBanner != null) {
context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
}

// Load the sources
Set<Object> sources = getSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[sources.size()]));
listeners.contextLoaded(context);
}
/**
* Load beans into the application context.
* @param context the context to load beans into
* @param sources the sources to load
*/
protected void load(ApplicationContext context, Object[] sources) {
if (logger.isDebugEnabled()) {
logger.debug(
"Loading source " + StringUtils.arrayToCommaDelimitedString(sources));
}
BeanDefinitionLoader loader = createBeanDefinitionLoader(
getBeanDefinitionRegistry(context), sources);
if (this.beanNameGenerator != null) {
loader.setBeanNameGenerator(this.beanNameGenerator);
}
if (this.resourceLoader != null) {
loader.setResourceLoader(this.resourceLoader);
}
if (this.environment != null) {
loader.setEnvironment(this.environment);
}
loader.load();
}
 
protected void postProcessApplicationContext(ConfigurableApplicationContext context) {
if (this.beanNameGenerator != null) {
context.getBeanFactory().registerSingleton(
AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,
this.beanNameGenerator);
}
if (this.resourceLoader != null) {
if (context instanceof GenericApplicationContext) {
((GenericApplicationContext) context)
.setResourceLoader(this.resourceLoader);
}
if (context instanceof DefaultResourceLoader) {
((DefaultResourceLoader) context)
.setClassLoader(this.resourceLoader.getClassLoader());
}
}
}





private void refreshContext(ConfigurableApplicationContext context) {
refresh(context);
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
}