SpringBoot之refresh方法讲解

1,767 阅读2分钟

refresh方法所在位置

image.png

方法代码展示

@Override
public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
    
      // Prepare this context for refreshing.
      //容器状态设置
      //初始化属性设置
      //检查必备属性是否存在
      prepareRefresh();

      // Tell the subclass to refresh the internal bean factory.
      // 设置beanFactory序列化id
      // 获取beanFactory
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // Prepare the bean factory for use in this context.
      // 设置beanFactory一些属性
      // 添加后置处理器
      // 设置忽略的自动装配接口
      // 注册一些组件
      prepareBeanFactory(beanFactory);

      try {
         // Allows post-processing of the bean factory in context subclasses.
         // 子类重写以在BeanFactory完成创建后做进一步设置
         postProcessBeanFactory(beanFactory);

         StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
         // Invoke factory processors registered as beans in the context.
         // 调用BeanDefinitionRegistryPostProcessor实现向容器内添加bean的定义
         // 调用BeanFactoryPostProcessor实现向容器内bean的定义的添加属性
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
         // 找到BeanPostProcessor的实现
         // 排序后注册进容器内
         registerBeanPostProcessors(beanFactory);
         beanPostProcess.end();

         // Initialize message source for this context.
         initMessageSource();

         // Initialize event multicaster for this context.
         // 初始化事件广播器
         initApplicationEventMulticaster();

         // Initialize other special beans in specific context subclasses.
         // 创建web容器
         onRefresh();

         // Check for listener beans and register them.
         // 添加容器内事件监听器至事件广播器中
         // 派发早期事件
         registerListeners();

         // Instantiate all remaining (non-lazy-init) singletons.
         // 初始化所有剩下的单实例bean
         finishBeanFactoryInitialization(beanFactory);

         // Last step: publish corresponding event.
         // 初始化生命周期处理器
         // 调用生命周期处理器onRefresh方法
         // 发布ContextRefreshedEvent事件
         // JMX相关处理
         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.
         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();
         contextRefresh.end();
      }
   }
}

我们可以看到方法体内使用synchronized,表明当前方法是个同步方法,在SpirngBoot启动当中,只允许一个线程进入到这个方法体内,来启动我们Spring容器。

invokeBeanFactoryPostProcessors的步骤

步骤一:

image.png

步骤二:

image.png

步骤三:

image.png

步骤四:

image.png