refresh 方法
refresh 方法概览
我们还是从 SpringApplication.run(String... args)
看起。
// SpringApplication # run(String... args) # 311-313
public ConfigurableApplicationContext run(String... args) {
// …………
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
// …………
return context;
}
我们进入 refreshContext(context)
方法体中:
// SpringApplication # refreshContext(ConfigurableApplicationContext context) # 390
private void refreshContext(ConfigurableApplicationContext context) {
refresh(context);
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
}
很显然,核心方法还是去调用了 refresh(context):
// SpringApplication # refresh(ApplicationContext applicationContext) # 742
protected void refresh(ApplicationContext applicationContext) {
Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
((AbstractApplicationContext) applicationContext).refresh();
}
继续进入((AbstractApplicationContext) applicationContext).refresh()
:
// AbstractApplicationContext # refresh() # 515
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 为刷新容器,准备上下文
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
// 告诉子类刷新内部bean工厂
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 准备在此上下文使用的bean工厂。
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 允许在上下文子类中对bean工厂进行后处理
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 调用在上下文中注册为bean的工厂处理器。
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 注册拦截Bean创建的Bean处理器。
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
// 初始化此上下文的消息源。
initMessageSource();
// Initialize event multicaster for this context.
// 为上下文,初始化事件广播器
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
// 在特定上下文子类中初始化其他特殊bean。
onRefresh();
// Check for listener beans and register them.
// 检查监听器并注册。
registerListeners