protected void refresh(ApplicationContext applicationContext) {

Assert. isInstanceOf(AbstractApplicationContext. class, applicationContex

t);

((AbstractAppl icationContext) applicat ionContext) .refresh();

}

}

其中 refresh 方法调用的是 AbstractApplicationContext 中的 refresh 方法,该类属于 spring-context 包。AbstractApplicationContext 的 refresh 方法更 多的是 Spring 相关的内容,这里我们通过 refresh 方法的顶层代码了解该方法都做了些什么,不过多深入 Spring 内部进行讲解。

blic void refresh() throws B

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

eansException, IllegalStateException {

//整个过程同步处理

synchronized (this. startupShutdownMonitor) {

//准备刷新工作

prepareRefresh();

//通知子类刷新内部 beanI 厂

ConfigurablelistableBeanFactory beanFactory = obtainFreshBeanFactory();

//为当前 context 准备 beanI 厂

prepareBeanF actory(beanFactory);

/允许 context 的子类对 beanIJ 进行后置处理

postProcessBeanFactory(beanFactory);

//调用 context 中注册为 bean 的工厂处理器

invokeBeanFactoryPostProcessors (beanFactory);

// 注册 bean 处理器(beanPostProcessors)

registerBeanPostProcessors (beanFactory);

//初始化 context 的信息源,和国际化有关

initMessageSource();

// 初始化 context 的專件传播器

ini tApplicationEventMulticaster();

//初始化其他子类特殊的 bean

onRefresh();

//检查并炷册事件监昕器

registerListeners();

//实例化所有非巅加裁单例

finishBeanFactoryInitialization(beanFactory);

//最后一步:发布对应事件

finishRefresh();

} catch (BeansException ex) {

// …

finally {

// …

}}}

在上面的代码中,调用 finishRefresh 方法初始化容器的生 命周期处理器并发布容器的生命周期事件之后,Spring 应用 上下文正式开启,Spring Boot 核心特性也随之启动。完成 refreshContext 方法操作之后,调用 afterRefresh 方 法。最新版本的 Spring Boot 中refreshContext 方法的实现默认为空,可由开发人员自行扩展,相关代码如下。

但需要注意的是,该方法在以往的版本中方法定义和实现差距较大,如果对此方法进行扩展,在升级版本时需特别留意。

protected void afterRefresh(ConfigurableApplicationContext context,

Applicat ionArguments args) {

//默认实现为空

}

完成以上操作之后,调用 SpringApplicationRun isteners 的 started 方法,通知监听器容器启动完成,并调用 ApplicationRunner 和 CommandL ineRunner 的运行方法。

调用 ApplicationRunner 和 CommandLineRunner

========================================

ApplicationRunner 和 CommandL ineRunner 是通过 SpringApplication 类的 callRunners方法来完成的,具体代码如下。

SpringBoot运行源码分析:Spring应用上下文刷新

private void callRunners (ApplicationContext context, Applicat ionArguments a

rgs) {

List<0bject> runners = new ArrayList<>();

runners . addAll(context. getBeansOfType(ApplicationRunner. class). values());

runners . addAll(context . getBeansOfType(CommandL ineRunner . class).values());

AnnotationAwareOrderComparator. sort (runners);

for (Object runner : new LinkedHashSet<>(runners)) {

if (runner instanceof ApplicationRunner) {

callRunner((ApplicationRunner) runner, args);

}

if (runner instanceof Commandl ineRunner)

callRunner( (CommandL ineRunner) runner, args);

private void callRunner(ApplicationRunner runner, ApplicationArguments arg

s)i

try {

(runner). run(args);

} catch (Exception ex) {

throw new IllegalStateException(“Failed to execute ApplicationRunner”,

ex);

private void callRunner (CommandL ineRunner runner, ApplicationArguments arg

s) {

//内容与上面方法相同

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐