主要作用是刷新应用上下文
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// 启动刷新的性能跟踪步骤
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
// 准备刷新,设置标志位,保存了容器的启动时间
prepareRefresh();
// 子类实现,获取一个新的 BeanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// 准备 BeanFactory,设置一些基本的配置
prepareBeanFactory(beanFactory);
try {
// 允许子类对 BeanFactory 进行进一步的处理
postProcessBeanFactory(beanFactory);
// 启动性能跟踪步骤,执行在容器中注册的 BeanFactoryPostProcessors
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
invokeBeanFactoryPostProcessors(beanFactory);
// 注册 BeanPostProcessors,这些 processors 会拦截 Bean 的创建过程
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
// 初始化消息源
initMessageSource();
// 初始化事件广播器
initApplicationEventMulticaster();
// 子类实现,初始化一些特殊的 beans
onRefresh();
// 注册监听器 beans
registerListeners();
// 实例化所有剩余的(非懒加载的)单例 beans
finishBeanFactoryInitialization(beanFactory);
// 最后一步:发布相应的事件
finishRefresh();
} catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// 销毁已创建的单例 beans,以避免悬空资源
destroyBeans();
// 重置 'active' 标志位
cancelRefresh(ex);
// 将异常传播给调用者
throw ex;
} finally {
// 重置 Spring 核心中的常见内省缓存,因为我们可能不再需要单例 beans 的元数据了
resetCommonCaches();
// 结束性能跟踪步骤
contextRefresh.end();
}
}
}
主要就做了这些,对上面的方法进行分解:
protected void prepareRefresh() {
// Switch to active.
this.startupDate = System.currentTimeMillis();
this.closed.set(false);
this.active.set(true);
//....省略部分代码
}
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// Tell the internal bean factory to use the context's class loader etc.
//设置类加载器
beanFactory.setBeanClassLoader(getClassLoader());
if (!shouldIgnoreSpel) {
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
}
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// Configure the bean factory with context callbacks.
//添加后置处理器:ApplicationContextAwareProcessor
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
//忽略自动装配
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
beanFactory.ignoreDependencyInterface(ApplicationStartupAware.class);
// BeanFactory interface not registered as resolvable type in a plain factory.
//以下接口,允许自动装配,第一个参数是自动装配的类型,,第二个字段是自动装配的值
// MessageSource registered (and found for autowiring) as a bean.
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// Register early post-processor for detecting inner beans as
//添加一个后置处理器:ApplicationListenerDetector,此后置处理器实现了BeanPostProcessor接口
ApplicationListeners.
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// Detect a LoadTimeWeaver and prepare for weaving, if found.
if (!NativeDetector.inNativeImage() && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// Register default environment beans.
//在容器中还没有XX的bean的时候,帮我们注册beanName为XX的singleton bean
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {
beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
}
}
主要逻辑代码在org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor>)
代码设计太多就不粘贴了,主要做了一下逻辑:
初始化设置:
ConfigurableListableBeanFactory
和一个 BeanFactoryPostProcessor
实例的列表。处理 BeanDefinitionRegistryPostProcessor
实例:
beanFactory
也是 BeanDefinitionRegistry
(用于管理 bean 定义的扩展接口),则该方法区分常规的 BeanFactoryPostProcessors
和实现了 BeanDefinitionRegistryPostProcessor
的处理器。beanFactoryPostProcessors
,对于实现了 BeanDefinitionRegistryPostProcessor
接口的实例,调用其 postProcessBeanDefinitionRegistry
方法,并将它们收集到不同的列表中。按顺序处理 BeanDefinitionRegistryPostProcessor
:
BeanDefinitionRegistryPostProcessor
:PriorityOrdered、Ordered 和其他处理器。postProcessBeanDefinitionRegistry
方法。处理已注册的 BeanFactoryPostProcessor
实例:
beanFactory
不是 BeanDefinitionRegistry
,则直接调用已注册的 BeanFactoryPostProcessor
实例。按顺序处理 BeanFactoryPostProcessor
:
PriorityOrdered
、Ordered
和其他接口的 BeanFactoryPostProcessor
分别处理。PriorityOrdered
接口的后置处理器。Ordered
接口的后置处理器。BeanFactoryPostProcessor
。清除缓存的合并 bean 定义: