AbstractApplicationContext#refresh方法

    @Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
       // 一些初始化,不重要
prepareRefresh(); // Tell the subclass to refresh the internal bean factory.
       // 获取beanFactory, 里面主要是BeanDefinitionMap 和BeanDefinitionName 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.
          // 调用所有BeanFactory后处理器: BeanFactoryPostProcessor,默认是空的,可以自己定义 invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.
// 注册bean后处理器,只是注册到beanFactory, 初始化方法前会调用所有的BeanPostProcessor,
// 初始化前后调用postProcessorBeforeInitialization/postProcessorAfterInitialization , aop重点看
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(); } } }

 

posted @ 2020-11-18 12:26  圣金巫灵  阅读(110)  评论(0编辑  收藏  举报