spring上下文刷新做了些什麼事情


    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.

               //後置處理bean工廠,可以在某些上下文實現中加特點的後置處理器,比如AbstractRefreshableWebApplicationContext中加入ServletContextAwareProcessor處理器和依賴忽略接口ServletContextAware。
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.

                //請求工廠後置處理器,包括上下文註冊的後置處理器以及註冊在工廠中處理器
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.

                //註冊bean後置處理器,這些處理器是以bean的形式註冊在工廠中的,要把他們拿出來放到工廠beanPostProcessors列表中。
                registerBeanPostProcessors(beanFactory);

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

                // Initialize event multicaster for this context.

                //到工廠中取出beanName爲applicationEventMulticaster的應用事件廣播器,所以其他bean不能取名爲applicationEventMulticaster
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.

                //模板方法可被特定的應用上下文重寫
                onRefresh();

                // Check for listener beans and register them.

                //把註冊在應用上下文中或者工廠中的ApplicationListener添加到廣播器
                registerListeners();

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

               //結束上下文初始化工作,實例化非延遲加載的單例bean
                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();
            }
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章