【SpringBoot】源码学习笔记(二)

【开始Run】

//计时器,当前线程的信息
StopWatch stopWatch = new StopWatch();
stopWatch.start();


//上下文
ConfigurableApplicationContext context = null;
//存储异常信息
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
//设置headless属性
this.configureHeadlessProperty();
//获取、启动监听器,SpringApplicationRunListener配置来源于springboot的spring.factories
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();

Collection exceptionReporters;
try {

//初始化默认应用参数
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

//根据监听器和默认应用参数,准备spring所需要的的环境
    ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
    this.configureIgnoreBeanInfo(environment);

//打印banner
    Banner printedBanner = this.printBanner(environment);

//创建应用上下文环境,区分不同的环境 [servlet、webflux、普通应用]
    context = this.createApplicationContext();

//异常报告
    exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);

//准备上下文,这里面涉及到了Spring相关的知识
    this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);

----------------------------------------------------prepareContext()--------------------------------------------------------

//将环境绑定到上下文
context.setEnvironment(environment);
//配置上下文bean的生成器和资源加载器
postProcessApplicationContext(context);
//为上下文采用所有初始化器
applyInitializers(context);
//触发监听器的contextPrepared事件
listeners.contextPrepared(context);
//记录启动的日志
if (this.logStartupInfo) {
   logStartupInfo(context.getParent() == null);
   logStartupProfileInfo(context);
}
// Add boot specific singleton beans 增加特殊的单例bean
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
   beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof DefaultListableBeanFactory) {
   ((DefaultListableBeanFactory) beanFactory)
         .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
// Load the sources 加载所有资源
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
//监听器触发contextLoaded事件
listeners.contextLoaded(context);

----------------------------------------------------prepareContext()--------------------------------------------------------

//刷新上下文
    this.refreshContext(context);

依次点进去,可以找到更深层的AbstractApplicationContext.refresh方法,这里也涉及到Spring相关的知识

----------------------------------------------------AbstractApplicationContext.refresh()-------------------------------------------

//contextd的设置:启动日期、当前状态、初始化环境等
this.prepareRefresh();
//创建一个bean工厂,去注册bean:定义、解析、处理和注册
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
//准备bean工厂
this.prepareBeanFactory(beanFactory);
try {
//注册scope 如request、session
    this.postProcessBeanFactory(beanFactory);
//调用所有bean工厂处理bean
    this.invokeBeanFactoryPostProcessors(beanFactory);
//bean的拦截操作
    this.registerBeanPostProcessors(beanFactory);
//国际化的操作
    this.initMessageSource();
//广播事件
    this.initApplicationEventMulticaster();
//处理特殊的bean,如tomcat
    this.onRefresh();
//注册监听器
    this.registerListeners();
//完成bean工厂的初始化
    this.finishBeanFactoryInitialization(beanFactory);
//完成刷新
    this.finishRefresh();
} catch (BeansException var9) {
    if (this.logger.isWarnEnabled()) {
        this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
    }
    this.destroyBeans();
    this.cancelRefresh(var9);
    throw var9;
} finally {
    this.resetCommonCaches();
}

----------------------------------------------------AbstractApplicationContext.refresh()-------------------------------------------

//刷新之后的操作
    this.afterRefresh(context, applicationArguments);

//停止计时器
    stopWatch.stop();

//日志输出
    if (this.logStartupInfo) {
        (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
    }

//启动监听器

    listeners.started(context);

//执行所有 Runner 运行器
    this.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;

【备注】

项目基于SpringBoot2.1.6

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章