Spring 源碼分析

爲了讀懂spring源碼,我是根據debug來分析的。
這裏寫圖片描述
一切的起點就是ApplicationContext。
根據構造函數的各種調用最終調用的是這個構造函數。

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

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

到這裏最終的方法就是refresh(),super只是做一些初始化工作,可以忽略。
以下是初始化發放做的事情:

DEBUG [main] - Adding [systemProperties] PropertySource with lowest search precedence
DEBUG [main] - Adding [systemEnvironment] PropertySource with lowest search precedence
DEBUG [main] - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]

沒有任何有用的信息,直接忽略。
setConfiguration()這是一個簡單的賦值操作。
而refresh()放最終調用的是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) {
                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;
            }
        }
    }

preparRefresh()的輸出

Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@26eeda37: startup date [Sun May 01 19:02:10 CST 2016]; root of context hierarchy
    // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

的輸出:

DEBUG [main] - Loading bean definitions
DEBUG [main] - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@26eeda37: org.springframework.beans.factory.support.DefaultListableBeanFactory@2582e0fc: defining beans [countryService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,dataSource,sqlSessionFactory,tk.mybatis.spring.mapper.MapperScannerConfigurer#0]; root of factory hierarchy

我首先從這個方法開始分析:

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

調用註冊在context裏面的beanFactory中的方法

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