spring 容器的refresh方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011686226/article/details/52767519

spring 的ApplicationContext 包含了beanFactory的所有功能,还有其他额外功能。

ClassPathXmlApplicationContext的父类AbstractApplicationContext的refresh()方法。

public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      // Prepare this context for refreshing.
      prepareRefresh();

      // Tell the subclass to refresh the internal bean factory.
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // Prepare the bean factory for use in this context.
      prepareBeanFactory(beanFactory);

      try {
         // Allows post-processing of the bean factory in context subclasses.
         postProcessBeanFactory(beanFactory);

         // Invoke factory processors registered as beans in the context.
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
         registerBeanPostProcessors(beanFactory);

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

         // Initialize event multicaster for this context.
         initApplicationEventMulticaster();

         // Initialize other special beans in specific context subclasses.
         onRefresh();

         // Check for listener beans and register them.
         registerListeners();

         // Instantiate all remaining (non-lazy-init) singletons.
         finishBeanFactoryInitialization(beanFactory);

         // Last step: publish corresponding event.
         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();
      }
   }
}


1.prepareBeanFactory方法,注册一个BeanPostProcessor。

beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

这个类的功能就是添加各种aware功能,比如ApplicationContextAware。

2.invokeBeanFactoryPostProcessors方法,调用BeanFactoryPostProcessors。

这个方法就把所有的BeanFactoryPostProcessor调用了一遍。

PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

2.1如果beanFactory是BeanDefinitionRegistry类型

遍历beanFactoryPostProcessors列表,如果是BeanDefinitionRegistryPostProcessor类型,强转在调用自定义的方法。

将预编码注册的BeanDefinitionRegistryPostProcessor(硬编码就是调用this.beanFactoryPostProcessors.add(postProcessor)方法放到beanFactoryPostProcessors中的)

放到registryPostProcessors

将硬编码注册的BeanFactoryPostProcessor放到regularPostProcessors中

就是BeanDefinitionRegistryPostProcessor还是BeanFactoryPostProcessor,如果是前者 先调用一下子类的方法,在全部调用父类的postProcessBeanFactory。

2.2如果beanFactory是BeanDefinitionRegistry类型

就直接调用postProcessBeanFactory方法。

3 registerBeanPostProcessors

这个方法就注册BeanPostProcessors,当你getbean的时候才会调用。



registerListeners()

将硬编码的事件监听器(在applicationListeners中),以及xml配置的事件监听器,以及earlyApplicationEvents中的事件放到事件广播器applicationEventMulticaster。

earlyApplicationEvents作用是当你调用publishEvent方法的时候,事件广播器还没有初始化的时候,会先放到publishEvent。



阅读更多
换一批

没有更多推荐了,返回首页