refresh方法在ConfigurableApplicationContext类中定义的具体实现是在AbstractApplicationContext中实现。这个方法的原文描述信息是:由于这是一个启动方法,如果它调用失败,它应该销毁已创建的单例,以避免悬空资源。换句话说,在调用该方法之后,应该实例化所有单例或者不是单例的对象。
在类ConfigurableApplicationContext中还定义了一个registerShutdownHook方法,这个方法是在JVM关闭的时候调用的用来销毁所有的bean。
接下来分析refresh方法
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
//----------------------------------------1--------------------------------------//
// 准备刷新和加载之前的上下文
prepareRefresh();
//----------------------------------------1--------------------------------------//
//----------------------------------------2--------------------------------------//
//告诉子类刷新内部bean工厂
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//----------------------------------------2--------------------------------------//
//----------------------------------------3--------------------------------------//
// 准备在此上下文中使用的bean工厂
prepareBeanFactory(beanFactory);
//----------------------------------------3--------------------------------------//
try {
//----------------------------------------4--------------------------------------//
//注册对应的BeanPostProcessor接口,就是ServletContextAwareProcessor
postProcessBeanFactory(beanFactory);
//----------------------------------------4--------------------------------------//
//----------------------------------------5--------------------------------------//
//调用注册的BeanFactoryPostProcessor的postProcessBeanDefinitionRegistry方法
invokeBeanFactoryPostProcessors(beanFactory);
//----------------------------------------5--------------------------------------//
//----------------------------------------6--------------------------------------//
// 注册拦截bean创建的bean处理器
registerBeanPostProcessors(beanFactory);
//----------------------------------------6--------------------------------------//
//初始化上下文的消息源。
initMessageSource();
//初始化上下文的事件传播器。
initApplicationEventMulticaster();
//在特定上下文子类中初始化其他特殊bean。
onRefresh();
// 检查监听器bean并注册它们
registerListeners();
//----------------------------------------7--------------------------------------//
//实例化所有剩余(非延迟初始化)单例。
finishBeanFactoryInitialization(beanFactory);
//----------------------------------------7--------------------------------------//
//----------------------------------------8--------------------------------------//
//最后一步:发布相应的事件。
finishRefresh();
//----------------------------------------8-------------------------------------//
}
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.
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();
}
}
}
方法1:prepareRefresh
protected void prepareRefresh() {
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());
}
}
//在上下文环境中初始化任何占位符属性源
initPropertySources();
// 验证指定的每个属性是不是可以解析的
getEnvironment().validateRequiredProperties();
// 初始化传播器
this.earlyApplicationEvents = new LinkedHashSet<>();
}
方法2:obtainFreshBeanFactory
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
//初始化BeanFactory
refreshBeanFactory();
//返回实例化的BeanFactory
return getBeanFactory();
}
其中的refreshBeanFactory方法的作用用来实例化BeanFactory并加载解析对应的Bean配置,其加载Bean的过程和前面讲到的BeanFactory加载的过程相同
protected final void refreshBeanFactory() throws BeansException {
//通过beanFactoryMonitor这个Bean工厂的监视器来判断是否已经实例化了BeanFactory
if (hasBeanFactory()) {
//如果实例化了BeanFactory,则先销毁所有的单例Bean
destroyBeans();
//关闭BeanFactory,也就是将beanFactory的值置为null
closeBeanFactory();
}
try {
//创建BeanFactory实例,如果实现了ConfigurableApplicationContext则返回父上下文的内部bean工厂;否则,返回父上下文本身
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
//如果用户自定义了允许Bean定义覆盖和允许循环引用则设置为定义的,默认为不允许
customizeBeanFactory(beanFactory);
//加载bean,前面已经讲过bean的加载
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
方法3:prepareBeanFactory
这个方法主要是配置BeanFactory的各种类加载器,需要的依赖和需要忽略的依赖,后处理器,解析器等
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
//设置加载Bean用的类加载器
beanFactory.setBeanClassLoader(getClassLoader());
//设置Bean中表达式解析用的解析器,默认的SpEL解析器
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
//添加属性注册时候的注册器,可以自定义注册器,只需要实现PropertyEditorRegistrar接口
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
//添加ApplicationContextAwareProcessor(其中包含了ApplicationContext,起到了传递的作用)
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
//忽略给定的自动装配依赖关系接口,放到ignoredDependencyInterfaces中保存
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 interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean.
//自动装配值注册特殊依赖关系类型,放到resolvableDependencies集合中保存
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
//注册ApplicationListenerDetector作为早期后处理器以检测内部bean实现了ApplicationListener的情况
// Register early post-processor for detecting inner beans as ApplicationListeners.
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// Detect a LoadTimeWeaver and prepare for weaving, if found.
//如果beanFactory中包含loadTimeWeaver名称的bean,则需要使用对应的临时的类加载器处理
if (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.
//注册默认的需要的上下文环境bean,包含environment,systemProperties和systemEnvironment
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());
}
}
方法4:postProcessBeanFactory
这个方法主要注册web请求相关的处理器和bean以及配置
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
//将ServletContextAwareProcessor注册到bean注册前处理器中
beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
//将ServletContextAware和ServletConfigAware设置为忽略依赖
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
//注册特定于Web的范围(如request,session和application),添加对应的web请求依赖(ServletRequest,ServletResponse,HttpSession,WebRequest使用各自对应的BeanFactory)
WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext);
//注册环境变量,在servletContext和servletConfig中设置的和自带的
WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext, this.servletConfig);
}
方法5:invokeBeanFactoryPostProcessors
这个方法主要是调用实现了的bean后处理器
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
/**
* 这里的调用所有的实现了BeanFactoryPostProcessor接口的类的方法,调用postProcessBeanDefinitionRegistry方法的时候按照会按照一定顺序来调用
* 1.调用实现PriorityOrdered的BeanDefinitionRegistryPostProcessors
* 2.调用实现Ordered的BeanDefinitionRegistryPostProcessors
* 3.调用所有其他BeanDefinitionRegistryPostProcessors,直到不再出现其他BeanDefinitionRegistryPostProcessors
* 调用postProcessBeanFactory没有顺序
*/
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
//这里特殊处理前面说到的bean名称为loadTimeWeaver的Bean
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}
方法6:registerBeanPostProcessors
这个方法主要是注册实现了的bean后处理器
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
/**
* 这里的注册所有的实现了BeanPostProcessor接口的类的方法一定顺序来注册
* 1.注册实现PriorityOrdered的BeanPostProcessor
* 2.注册实现Ordered的BeanPostProcessor
* 3.注册所有其他BeanPostProcessor,直到不再出现其他BeanPostProcessor
* 最后,重新注册ApplicationListenerDetector为后处理器以检测内部bean实现ApplicationListener接口的,并将其移动到处理器链的末尾
*/
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
方法7:finishBeanFactoryInitialization
在这里加载并实例化所有的懒加载的bean
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
//设置对应的用于类型转换的服务接口,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.*/
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
//停止使用临时ClassLoader进行类型匹配,因为已经加载完了bean
beanFactory.setTempClassLoader(null);
//冻结所以的bean定义,说明注册的bean定义将不被修改或任何进一步的处理
beanFactory.freezeConfiguration();
//初始化剩下的单实例(非惰性),在这个方法中最终会调用getBean方法,就会实例化bean
beanFactory.preInstantiateSingletons();
}
方法8:finishRefresh
protected void finishRefresh() {
//清除上下文级资源缓存
clearResourceCaches();
// 为此上下文初始化生命周期处理器,可以自定义LifecycleProcessor,如果没有则使用默认的DefaultLifecycleProcessor
initLifecycleProcessor();
// 刷新bean的生命周期
getLifecycleProcessor().onRefresh();
// Publish the final event.
publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.
LiveBeansView.registerApplicationContext(this);
}