Spring源码阅读——ApplicationContext

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_18860653/article/details/81083890

Spring中提供了一个接口ApplicationContext,用于扩展BeanFactory中现有的功能。它提供了更多的功能。现在我们来看下它的实现:

    public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
            throws BeansException {

        super(parent);
        setConfigLocations(configLocations);
        if (refresh) {
            refresh();
        }
    }

其中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.
            //初始化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.
                //子类覆盖方法做额外处理
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                //激活各种BeanFactory处理器
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                //注册拦截Bean创建的Bean处理器,这里只是注册,真正地调用在getBean的时候,比如配置文件通过${}读取等
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                //为上下文初始化Message源,(比如国际化处理)
                initMessageSource();

                // Initialize event multicaster for this context.
                //初始化应用消息广播,并放入 applicationEventMulticaster bean中
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                //留给子类来初始化其它的bean
                onRefresh();

                // Check for listener beans and register them.
                //在所有注册的bean中查找Listener bean,注册到消息广播器中
                registerListeners();

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

                //完成刷新过程,通知生命周期护处理器lifecycleProcessor刷新过程,同时发出ContextRefreshEvent通知别人(LifecycleProcessor 用来与所有声明的bean的周期做状态更新)
                // 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();
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_18860653/article/details/81083890