美文网首页
Spring中ApplicationContext初始化过程

Spring中ApplicationContext初始化过程

作者: Solace_0e71 | 来源:发表于2017-08-10 21:53 被阅读0次

    Spring在初始化过程中要做的事情很多,下面我们就根据ClassPathXmlApplicationContext初始化看看我们的应用走了哪些步骤,我用debug模式下一步步来展现初始化过程。
    首先我们看一下ClassPathXmlApplicationContext类的继承关系

    ClassPathXmlApplicationContext
    AbstractXmlApplicationContext
    AbstractRefreshableConfigApplicationContext
    AbstractRefreshableApplicationContext
    AbstractApplicationContext
    DefaultResourceLoader
    I ResourceLoad
    
    ApplicationContext context = new ClassPathXmlApplicationContext("conf/applicationContext.xml");
    public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
                throws BeansException {
            super(parent);
            setConfigLocations(configLocations);
            if (refresh) {
                refresh();
            }
        }
    

    初始化应用上下文,使用ClassPathXmlApplicationContext类新建实例,初始化这个类,我们可以看到使用super方法初始化了父类,上面继承树中所有的类都初始化了;然后把传送的配置文件目录设置为配置文件;调用refresh方法,这个方法是AbstractApplicationContext类实现的方法。

    public void refresh() throws BeansException, IllegalStateException {
            synchronized (this.startupShutdownMonitor) {
                // Prepare this context for refreshing.
    //准备上下文的刷新,
                prepareRefresh();
                // Tell the subclass to refresh the internal bean factory.
    //得到新的Bean工厂,应用上下文加载bean就是在这里面实现的
                ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    //准备bean工厂用在上下文中
                // Prepare the bean factory for use in this context.
                prepareBeanFactory(beanFactory);
                try {
                    // Allows post-processing of the bean factory in context subclasses.
    //允许子类上下问处理bean工厂
                    postProcessBeanFactory(beanFactory);
                    // Invoke factory processors registered as beans in the context.
    //请求工厂处理器作为beans注册在上下文
                    invokeBeanFactoryPostProcessors(beanFactory);
                    // Register bean processors that intercept bean creation.
    //注册bean处理器拦截bean创建
                    registerBeanPostProcessors(beanFactory);
                    // Initialize message source for this context.
    //初始化上下文中消息源
                    initMessageSource();
                    // Initialize event multicaster for this context.
    //初始化上下文中事件广播
                    initApplicationEventMulticaster();
                    // Initialize other special beans in specific context subclasses.
    //初始化其他具体bean
                    onRefresh();
                    // Check for listener beans and register them.
    //检查监听bean并注册
                    registerListeners();
                    // Instantiate all remaining (non-lazy-init) singletons.
    //实例化未初始化单例
                    finishBeanFactoryInitialization(beanFactory);
                    // Last step: publish corresponding event.
    //最后一步发布相应事件
                    finishRefresh();
                }
    

    obtainFreshBeanFactory方法把配置文件中的bean加载到容器中。调用AbstractRefreshableApplicationContext的refreshBeanFactory方法,然后调用loadBeanDefinitioans(beanFactory)方法,这个方法的实现类是XmlWebApplicationContext的loadBeanDefinitions方法,方法调用Reader中的loadBeanDefinitions(configLocations)把配置文件传入Reader。

    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
    //这里定义了Reader来读取Bean,在loadBeanDifinitions方法中传入reader
            XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    ......
            // then proceed with actually loading the bean definitions.
            initBeanDefinitionReader(beanDefinitionReader);
            loadBeanDefinitions(beanDefinitionReader);
        }
    

    XmlBeanDefinitionReader中调用AbstractBeanDefinition中的loadBeanDefinitions(location)方法。把配置文件读取成Resource后,调用XmlBeanDefinitionReader中loadBeanDefinitions(Resource)方法,在方法中调用doLoadBeanDefinitions(InputSource inputSource, Resource resource)传入参数是文件流和Resource,方法内调用registerBeanDefinitions(Document doc,Resource resource)方法

    public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
            BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
            documentReader.setEnvironment(this.getEnvironment());
            int countBefore = getRegistry().getBeanDefinitionCount();
            documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
            return getRegistry().getBeanDefinitionCount() - countBefore;
        }
    

    在DocumentReader调用注册bean方法中进行注册,调用DefaultBeanDefinitionDocumentReader的doRegisterBeanDefinitions方法,在parseBeanDefinitions方法中调用parseDefaultElement(ele, delegate);方法,判断如果获取到的是个bean的话执行,processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate)方法最后在DefaultListableBeanFactory类中调用registerBeanDefinition进行注册,注册的容器是this.beanDefinitionMap.put(beanName, beanDefinition);,这个是初始化的CurrentHashMap类。
    容器的初始化以及Bean的注册就完成了。

    相关文章

      网友评论

          本文标题:Spring中ApplicationContext初始化过程

          本文链接:https://www.haomeiwen.com/subject/cjktrxtx.html