1. 程式人生 > >spring原始碼學習之路---深度分析IOC容器初始化過程(三)

spring原始碼學習之路---深度分析IOC容器初始化過程(三)

  • 分析FileSystemXmlApplicationContext的建構函式,到底都做了什麼,導致IOC容器初始化成功。
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
            throws BeansException {

        super(parent);
        setConfigLocations(configLocations);
        if
(refresh) { refresh(); } }
  • 我們跟蹤上一章FileSystemXmlApplicationContext的建構函式,可以發現它最終呼叫的是上面這個形式過載的建構函式,其中的refresh方法,便是IOC容器初始化的入口。下面我們繼續跟蹤程式碼進去看一下refresh方法。refresh方法位於AbstractApplicationContext中,這是一個抽象類,初步實現了ApplicationContext的一般功能,並且這裡使用了模板模式,給以後要實現的子類提供了統一的模板。
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) { // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } } }
  • 這裡面列出了IOC容器初始化的大致步驟,第一步很容易看出來是初始化準備,這個方法裡只是設定了一個活動標識,我們主要來看第二步,obtainFreshBeanFactory這個方法,它是用來告訴子類重新整理內部的bean工廠,接下來我們跟蹤進去看看。
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
        refreshBeanFactory();
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        if (logger.isDebugEnabled()) {
            logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
        }
        return beanFactory;
    }
  • 該方法中第一句便呼叫了另外一個refreshBeanFactory方法,這個方法是AbstractApplicationContext中的抽象方法,具體的實現並沒有在這個抽象類中實現,而是留給了子類,我們追蹤到這個子類當中去看一下。該方法又子類AbstractRefreshableApplicationContext實現,我們來看
protected final void refreshBeanFactory() throws BeansException {
        if (hasBeanFactory()) {
            destroyBeans();
            closeBeanFactory();
        }
        try {
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            beanFactory.setSerializationId(getId());
            customizeBeanFactory(beanFactory);
            loadBeanDefinitions(beanFactory);
            synchronized (this.beanFactoryMonitor) {
                this.beanFactory = beanFactory;
            }
        }
        catch (IOException ex) {
            throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
        }
    }
  • 方法加上了final關鍵字,也就是說此方法不可被重寫,可以很清楚的看到,IOC容器的初始化就是在這個方法裡發生的,第一步先是判斷有無現有的工廠,有的話便會將其摧毀,否則,就會建立一個預設的bean工廠,也就是前面提到的DefaultListableBeanFactory,注意看loadBeanDefinitions(beanFactory);這裡,當我們建立了一個預設的bean工廠以後,便是載入bean的定義。這與我們上一章所使用的原始的建立bean工廠的方式極為相似。看到這裡,其實不難看出,FileSystemXmlApplicationContext的初始化方法中,其實已經包含了我們上一章當中原始的建立過程,這個類是一個現有的,spring已經為我們實現好的BeanFactory的實現類。在專案當中,我們經常會用到。

轉載http://www.cnblogs.com/zuoxiaolong/p/spring4.html