Spring 5.1.x 容器的刷新过程
概述
Spring容器体系的继承结构图如下:
从图中可以看出抽象类AbstractApplicationContext是所有容器类的父类。AbstractApplicationContext实现了它继承的所有接口方法,并定义了三个用于管理BeanFactory对象的抽象方法,如下:
//---------------------------------------------------------------------
// Abstract methods that must be implemented by subclasses
//---------------------------------------------------------------------
/**
* Subclasses must implement this method to perform the actual configuration load.
* The method is invoked by {@link #refresh()} before any other initialization work.
* <p>A subclass will either create a new bean factory and hold a reference to it,
* or return a single BeanFactory instance that it holds. In the latter case, it will
* usually throw an IllegalStateException if refreshing the context more than once.
* @throws BeansException if initialization of the bean factory failed
* @throws IllegalStateException if already initialized and multiple refresh
* attempts are not supported
*/
protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;
/**
* Subclasses must implement this method to release their internal bean factory.
* This method gets invoked by {@link #close()} after all other shutdown work.
* <p>Should never throw an exception but rather log shutdown failures.
*/
protected abstract void closeBeanFactory();
/**
* Subclasses must return their internal bean factory here. They should implement the
* lookup efficiently, so that it can be called repeatedly without a performance penalty.
* <p>Note: Subclasses should check whether the context is still active before
* returning the internal bean factory. The internal factory should generally be
* considered unavailable once the context has been closed.
* @return this application context's internal bean factory (never {@code null})
* @throws IllegalStateException if the context does not hold an internal bean factory yet
* (usually if {@link #refresh()} has never been called) or if the context has been
* closed already
* @see #refreshBeanFactory()
* @see #closeBeanFactory()
*/
@Override
public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
PS:告诉大家如何查看一个抽象类未实现的接口方法有哪些,比如上面的AbstractApplicationContext抽象类。我创建了一个MyselfApplicationContext 类,如果未实现任何方法,IDE会提醒我实现父类中未实现的方法,代码如下:
public class MyselfApplicationContext extends AbstractApplicationContext {
@Override
protected void refreshBeanFactory() throws BeansException, IllegalStateException {
}
@Override
protected void closeBeanFactory() {
}
@Override
public ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException {
return null;
}
}
Spring容器在初始化的时候都会执行一个方法来结束初始化,那就是refresh()方法,此方法定义在接口ConfigurableApplicationContext中,并且抽象类AbstractApplicationContext实现了它,代码如下:
/**
* Spring容器在初始化的时候都会执行一个方法来结束初始化,那就是refresh()方法,
* 此方法定义在接口ConfigurableApplicationContext中,并且抽象类AbstractApplicationContext实现了它
*/
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 为容器的刷新做准备工作,设置启动日期,更新活动标志,加载属性资源
//第1步:容器刷新前准备
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
// 获取最新的bean工厂对象。
// 要求子类刷新自身持有的bean工厂对象
//第2步:创建BeanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 在容器使用bean工厂之前,设置bean工厂,主要是添加特定的bean后处理器
//为bean工厂设置容器的类加载器,以及一些特定的bean后处理器(也可以这样理解)
//第3步:初始化BeanFactory
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 钩子方法,允许在子类中bean工厂做进一步的处理
// 子类可以根据自身需要,添加更多的bean后处理器
//在web应用中,AbstractRefreshableWebApplicationContext类重写此方法
//第4步:执行BeanFactoryPostProcessor接口的postProcessBeanFactory方法
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 在上下文容器中执行所有注册了的bean工厂后处理器(完成容器中所有bean工厂后处理器的执行)
// 执行步骤:1. 在bean工厂中找到所有实现了BeanFactoryPostProcessor的对象
// ->2. 执行所有bean工厂后处理器的postProcessBeanFactory方法
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 把所有用户配置的bean后处理器添加到bean工厂中
//第5步:注册BeanPostProcessor
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
// 初始化国际化消息对象,默认为DelegatingMessageSource
//第6步:初始化MessageSource国际化消息
initMessageSource();
// Initialize event multicaster for this context.
// 第7步:初始化事件传播器,默认为SimpleApplicationEventMulticaster
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
// 钩子方法,初始化其他特定的bean
// 第8步:其他初始化
onRefresh();
// Check for listener beans and register them.
// 第9步:注册监听器
// 注册用户指定的和容器创建时添加的监听器
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
// 第10步:实例化非延迟单例bean
// 初始化所有剩下的单例非延迟bean
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
// 第11步:完成刷新发布刷新事件
// 完成刷新,包括发布容器刷新事件
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
// 销毁所有已经创建好的bean,以防止内存浪费
destroyBeans();
// Reset 'active' flag.
// 重置活动标志
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
// 重置反射缓存
resetCommonCaches();
}
}
}
refresh方法的设计使用了模板方法设计模式,它设定了容器刷新的流程,并把特定阶段的执行延迟到子类中执行,比如bean工厂的创建。它还在特定的阶段提供钩子方法,以方便子类根据自身需要进一步完成更多的操作。下面我们一步一步的探讨refresh方法的执行流程。下图是它的一个大概流程图。
refresh方法在容器的基本配置完成后被调用,它是spring容器初始化过程的主要战场,下面我们一步一步的探讨refresh方法的执行流程。
容器刷新过程
1. 容器刷新前准备
在上边refresh()方法的第1步中,调用AbstractApplicationContext的prepareRefresh()方法完成容器刷新前的准备,代码如下:
protected void prepareRefresh() {
// Switch to active.
// 设置启动时间
this.startupDate = System.currentTimeMillis();
this.closed.set(false);
// 把容器设置为活动
this.active.set(true);
if (logger.isDebugEnabled()) {
if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
}
// Initialize any placeholder property sources in the context environment.
// 钩子方法,让子类把所有的stub属性资源替换成真正需要的属性资源
initPropertySources();
// Validate that all properties marked as required are resolvable:
// see ConfigurablePropertyResolver#setRequiredProperties
// 验证所有被标注为required的属性是否可被解析
getEnvironment().validateRequiredProperties();
// Store pre-refresh ApplicationListeners...
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
// Reset local application listeners to pre-refresh state.
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
// Allow for the collection of early ApplicationEvents,
// to be published once the multicaster is available...
// 允许事件传播器可用的时候发布一些事件
// earlyApplicationEvents存储需要在事件传播器可用时发布的事件
this.earlyApplicationEvents = new LinkedHashSet<>();
}
prepareRefresh方法设置容器启动时间和活动标志,以及通过调用initPropertySources()方法完成所有的property资源的初始化。initPropertySources()方法是一个空实现,子类可以用它来完成property资源的初始化。例如,下面代码是在AbstractRefreshableWebApplicationContext类中的实现:
/**
* 这个方法主要是把ServletContext和ServletConfig对象保存到ConfigurableWebEnvironment环境对象中,
* 以方便环境对象通过getProperty(String key)方法获取定义在ServletContext和ServletConfig中特定的初始化init-param参数
*/
@Override
protected void initPropertySources() {
ConfigurableEnvironment env = getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
}
}
2. 创建BeanFactory
就是执行AbstractApplicationContext的obtainFreshBeanFactory方法,获取最新的bean工厂对象,代码如下:
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
// 刷新bean工厂,是一个抽象方法,由子类实现
//refreshBeanFactory()抽象方法在GenericApplicationContext类和AbstractRefreshableApplicationContext类中都有实现
refreshBeanFactory();
//add by yanglin
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + getBeanFactory());
}
// 从子类中获取bean工厂,也是一个抽象方法,由子类实现
return getBeanFactory();
}
obtainFreshBeanFactory方法首先调用子类的refreshBeanFactory()方法刷新bean工厂,然后调用子类的getBeanFactory()方法获取bean工厂。在spring中,refreshBeanFactory()在GenericApplicationContext类和AbstractRefreshableApplicationContext类中都有实现,下面是在AbstractRefreshableApplicationContext类中的实现。
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
// 如果当前上下文容器已有bean工厂
// 销毁bean工厂中所有单例bean
destroyBeans();
// 关闭已有的bean工厂
closeBeanFactory();
}
try {
// 创建一个新的bean工厂
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
//自定义bean工厂,包括设置是否允许覆盖相同ID的bean,是否允许循环引用,等等
customizeBeanFactory(beanFactory);
// 加载所有BeanDefinition,是一个抽象方法,有子类实现
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
在这个方法里面,首先把已创建的单例bean销毁并关闭持有的bean工厂,然后调用createBeanFactory()方法重新创建一个x新的bean工厂代码如下。最后,并调用子类的loadBeanDefinitions方法把BeanDefinition对象加载到bean工厂中。
/**
* 创建一个新的bean工厂
**/
protected DefaultListableBeanFactory createBeanFactory() {
return new DefaultListableBeanFactory(getInternalParentBeanFactory());
}
3. 初始化BeanFactory
4. 执行bean工厂后处理器的postProcessBeanFactory方法
5. 注册BeanPostProcessor
6. 初始化MessageSource国际化消息
就是执行AbstractApplicationContext的initMessageSource方法,代码如下:
protected void initMessageSource() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
// 声明:public static final String MESSAGE_SOURCE_BEAN_NAME = "messageSource";
// 检测用户是否指定了messageSource
if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
// Make MessageSource aware of parent MessageSource.
// 使当前messageSource能够使用父容器中的messageSource
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
if (hms.getParentMessageSource() == null) {
// Only set parent context as parent MessageSource if no parent MessageSource
// registered already.
hms.setParentMessageSource(getInternalParentMessageSource());
}
}
if (logger.isTraceEnabled()) {
logger.trace("Using MessageSource [" + this.messageSource + "]");
}
}
else {
// Use empty MessageSource to be able to accept getMessage calls.
// 用户没有指定messageSource
// 使用一个空的MessageSource,以能够接受getMessage方法的调用
DelegatingMessageSource dms = new DelegatingMessageSource();
dms.setParentMessageSource(getInternalParentMessageSource());
this.messageSource = dms;
beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
if (logger.isTraceEnabled()) {
logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
}
}
}
PS:如果要使用国际化消息,必须提供bean名称为messageSource的MessageSource对象,形如下面的配置:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>/WEB-INF/languages/globe/messages</value>
</list>
</property>
<property name="cacheSeconds" value="1800"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
7. 初始化事件传播器
refresh()方法完成国际化消息对象初始化后,接着调用AbstractApplicationContext的initApplicationEventMulticaster方法为容器添加事件传播器,代码如下:
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
// 声明:public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";
// 检测用户是否制定了事件传播器
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isTraceEnabled()) {
logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
// 使用默认的事件传播器
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) {
logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
}
}
}
8. 其他初始化
refresh()方法完成国际化消息和事件传播器的初始化后,把容器的初始化交给onRefresh()方法,代码如下:
protected void onRefresh() throws BeansException {
// For subclasses: do nothing by default.
}
onRefresh()方法被设计成容器初始化的钩子,以方便子类初始化其他特定的bean。例如,下面是在AbstractRefreshableWebApplicationContext类中onRefresh()方法被重写的代码:
/**
* Initialize the theme capability.
*/
@Override
protected void onRefresh() {
this.themeSource = UiApplicationContextUtils.initThemeSource(this);
}
其中,UiApplicationContextUtils工具类的initThemeSource(ApplicationContext context)方法的代码如下:
public static ThemeSource initThemeSource(ApplicationContext context) {
// 声明有:public static final String THEME_SOURCE_BEAN_NAME = "themeSource";
if (context.containsLocalBean(THEME_SOURCE_BEAN_NAME)) {
ThemeSource themeSource = context.getBean(THEME_SOURCE_BEAN_NAME, ThemeSource.class);
// Make ThemeSource aware of parent ThemeSource.
// 使themeSource关联父容器,这个父容器必须实现了ThemeSource接口
if (context.getParent() instanceof ThemeSource && themeSource instanceof HierarchicalThemeSource) {
HierarchicalThemeSource hts = (HierarchicalThemeSource) themeSource;
if (hts.getParentThemeSource() == null) {
// Only set parent context as parent ThemeSource if no parent ThemeSource
// registered already.
hts.setParentThemeSource((ThemeSource) context.getParent());
}
}
if (logger.isDebugEnabled()) {
logger.debug("Using ThemeSource [" + themeSource + "]");
}
return themeSource;
}
else {
// Use default ThemeSource to be able to accept getTheme calls, either
// delegating to parent context's default or to local ResourceBundleThemeSource.
HierarchicalThemeSource themeSource = null;
if (context.getParent() instanceof ThemeSource) {
// 把对ThemeSource的操作代理给父容器
themeSource = new DelegatingThemeSource();
themeSource.setParentThemeSource((ThemeSource) context.getParent());
}
else {
// 使用默认的ThemeSource
themeSource = new ResourceBundleThemeSource();
}
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate ThemeSource with name '" + THEME_SOURCE_BEAN_NAME +
"': using default [" + themeSource + "]");
}
return themeSource;
}
}
9. 注册监听器
执行完成子类实现的onRefresh()方法,需要向容器中注册各种监听器,此时执行AbstractApplicationContext的registerListeners()方法,这个方法的代码如下:
protected void registerListeners() {
// Register statically specified listeners first.
// 首先注册容器中特定的事件监听器
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
// 找到用户配置的所有的监听器bean名称
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
// 在事件传播器中只保存监听器的bean名称
// 这里不实例化监听器的原因是为了让后处理器在它们真正实例化的时候作用于它们
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...
// 发布那些需要提前发布的事件
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
10. 实例化非延迟单例bean
当前面所有的初始化操作都完成了后,则开始实例化剩下的单例非延迟加载的bean。通过执行AbstractApplicationContext的finishBeanFactoryInitialization方法完成,这个方法的代码如下:
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// Initialize conversion service for this context.
// 初始化ConversionService
// 在ConfigurableApplicationContext接口中声明:String CONVERSION_SERVICE_BEAN_NAME = "conversionService";
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// Register a default embedded value resolver if no bean post-processor
// (such as a PropertyPlaceholderConfigurer bean) registered any before:
// at this point, primarily for resolution in annotation attribute values.
// 注册一个默认的内部value解析器
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
// 初始化LoadTimeWeaverAware对象来支持注册他们的transformers
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
// 停止使用临时类加载器来做类型匹配
beanFactory.setTempClassLoader(null);
// Allow for caching all bean definition metadata, not expecting further changes.
// 允许缓存所有的bean元数据定义,不希望今后再更改
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
// 初始化所有非延迟单例bean
beanFactory.preInstantiateSingletons();
}
11. 完成刷新
refresh()方法的最后一步,AbstractApplicationContext的finishRefresh方法,完成容器刷新,执行LifecycleProcessor对象的onRefresh方法,以及发布刷新事件。代码如下:
protected void finishRefresh() {
// Clear context-level resource caches (such as ASM metadata from scanning).
//清除caches
clearResourceCaches();
// Initialize lifecycle processor for this context.
// 初始化生命周期处理器,默认为DefaultLifecycleProcessor
initLifecycleProcessor();
// Propagate refresh to lifecycle processor first.
// 首先把刷新操作传递给生命周期处理器
getLifecycleProcessor().onRefresh();
// Publish the final event.
// 最后发布容器刷新事件
publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.
// 把spring容器加入到LiveBeansView的MBeanServer中
LiveBeansView.registerApplicationContext(this);
}
在finishRefresh() 方法中,initLifecycleProcessor方法是初始化生命周期处理器。代码为:
protected void initLifecycleProcessor() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
// 声明:public static final String LIFECYCLE_PROCESSOR_BEAN_NAME = "lifecycleProcessor";
if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
// 使用用户指定的生命周期处理器
this.lifecycleProcessor =
beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
if (logger.isTraceEnabled()) {
logger.trace("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");
}
}
else {
// 使用默认的声明周期处理器
DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
defaultProcessor.setBeanFactory(beanFactory);
this.lifecycleProcessor = defaultProcessor;
beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
if (logger.isTraceEnabled()) {
logger.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "' bean, using " +
"[" + this.lifecycleProcessor.getClass().getSimpleName() + "]");
}
}
}
至此spring的刷新也就完成了。