1. 程式人生 > >Spring源碼:IOC原理解析(二)

Spring源碼:IOC原理解析(二)

main 節點 定義 nat ner multicast esp loading more

版權聲明:本文為博主原創文章,轉載請註明出處,歡迎交流學習!

接著上一章節的內容,我們來分析當new一個FileSystemXmlApplicationContext對象的時候,spring到底做了那些事。FileSystemXmlApplicationContext類的內容主要是定義了若幹重載的構造方法,核心構造方法如下:

/**
     * Create a new FileSystemXmlApplicationContext with the given parent,
     * loading the definitions from the given XML files.
     * 
     * loading all bean definitions and creating all singletons.
     * Alternatively, call refresh manually after further configuring the context.
     * 
     
*/ public FileSystemXmlApplicationContext( String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }

從方法說明可以看出,在這個構造方法裏加載所有bean定義並創建bean單例實例。其中的refresh()方法就是IOC容器初始化的入口,refresh()方法位AbstractApplicationContext類中,這是一個抽象類,它實現了ApplicationContext的基礎功能,這裏使用了模版方法模式,給實現它的子類提供了統一的模板:

@Override
    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工廠 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) { 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(); } } }

refresh()方法裏列出了IOC容器初始化的步驟,第一個方法是初始化準備,這裏只是設置啟動日期和活動標識以及執行屬性源的初始化。我們重點看第二個方法obtainFreshBeanFactory(),它告訴子類刷新內部bean工廠,返回了一個ConfigurableListableBeanFactory,跟蹤這個方法:

/**
     * Tell the subclass to refresh the internal bean factory.
     * @return the fresh BeanFactory instance
     * @see #refreshBeanFactory()
     * @see #getBeanFactory()
     */
    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
        refreshBeanFactory();
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        if (logger.isDebugEnabled()) {
            logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
        }
        return beanFactory;
    }
protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

/**
     * Return the internal bean factory of this application context.
     * Can be used to access specific functionality of the underlying factory.
     * 
     */
    ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

obtainFreshBeanFactory()方法的第一行調用了refreshBeanFactory()方法,這是一個抽象方法,由它的子類來實現,方法的第二行調用了getBeanFactory(),這是在其父接口中定義的一個空方法。抽象方法refreshBeanFactory()在其子類子類AbstractRefreshableApplicationContext中實現:

/**
     * This implementation performs an actual refresh of this context‘s underlying
     * bean factory, shutting down the previous bean factory (if any) and
     * initializing a fresh bean factory for the next phase of the context‘s lifecycle.
     * 
     * 此實現執行該上下文的底層bean工廠的實際刷新,關閉以前的bean工廠(如果有的話),
     * 並為上下文生命周期的下一階段初始化一個新的bean工廠 
     */
    @Override
    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容器的初始化就是在這個方法中完成的。第一步先判斷有沒有現有的工廠,有的話就銷毀掉,然後創建一個默認的工廠,也就是DefaultListableBeanFactory ,接下來兩行代碼是設置bean工廠的一些屬性,註意看loadBeanDefinitions(beanFactory)這行,當創建了一個默認的bean工廠後,加載bean定義,這跟我們上一章節使用原始方式初始化bean工廠類似。從這裏不難看出,FileSystemXmlApplicationContext的構造方法中其實已經包含了我們上一章節中原始的初始化過程。接下來我們跟蹤一下loadBeanDefinitions(beanFactory)的實現,這個方法是由AbstractXmlApplicationContext抽象類實現的:

/**
     * Loads the bean definitions via an XmlBeanDefinitionReader.裝載bean定義通過XmlBeanDefinitionReader
     *
     */
    @Override
    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
        // Create a new XmlBeanDefinitionReader for the given BeanFactory.
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

        // Configure the bean definition reader with this context‘s
        // resource loading environment.
        beanDefinitionReader.setEnvironment(this.getEnvironment());
        beanDefinitionReader.setResourceLoader(this);
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

        // Allow a subclass to provide custom initialization of the reader,
        // then proceed with actually loading the bean definitions.
        initBeanDefinitionReader(beanDefinitionReader);
        loadBeanDefinitions(beanDefinitionReader);
    }

方法的第一行首先定義了一個Reader,這個Reader就是用來讀取xml配置文件的,最後一行就是真正載入bean定義的實現過程,代碼如下:

/**
     * Load the bean definitions with the given XmlBeanDefinitionReader.
     * 
     */
    protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
        Resource[] configResources = getConfigResources();
        if (configResources != null) {
            reader.loadBeanDefinitions(configResources);
        }
        String[] configLocations = getConfigLocations();
        if (configLocations != null) {
            reader.loadBeanDefinitions(configLocations);
        }
    }

上面的方法調用了XmlBeanDefinitionReader類的loadBeanDefinitions(EncodedResource encodedResource)方法:

/**
     * Load bean definitions from the specified XML file.
     * rows BeanDefinitionStoreException in case of loading or parsing errors
     */
    public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
        Assert.notNull(encodedResource, "EncodedResource must not be null");
        if (logger.isInfoEnabled()) {
            logger.info("Loading XML bean definitions from " + encodedResource.getResource());
        }

        Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
        if (currentResources == null) {
            currentResources = new HashSet<>(4);
            this.resourcesCurrentlyBeingLoaded.set(currentResources);
        }
        if (!currentResources.add(encodedResource)) {
            throw new BeanDefinitionStoreException(
                    "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
        }
        try {
            InputStream inputStream = encodedResource.getResource().getInputStream();
            try {
                InputSource inputSource = new InputSource(inputStream);
                if (encodedResource.getEncoding() != null) {
                    inputSource.setEncoding(encodedResource.getEncoding());
                }
                return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
            }
            finally {
                inputStream.close();
            }
        }
        catch (IOException ex) {
            throw new BeanDefinitionStoreException(
                    "IOException parsing XML document from " + encodedResource.getResource(), ex);
        }
        finally {
            currentResources.remove(encodedResource);
            if (currentResources.isEmpty()) {
                this.resourcesCurrentlyBeingLoaded.remove();
            }
        }
    }

從方法說明可以看出,這個方法是從指定的xml文件中加載bean定義,try塊中的代碼才是載入bean定義的過程。spring將資源返回的輸入流包裝以後傳給了doLoadBeanDefinitions()方法,我們進入這個方法,代碼如下:

/**
     * Actually load bean definitions from the specified XML file.
     * 
     */
    protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
            throws BeanDefinitionStoreException {
        try {
            Document doc = doLoadDocument(inputSource, resource);
            return registerBeanDefinitions(doc, resource);
        }
        catch (BeanDefinitionStoreException ex) {
            throw ex;
        }
        catch (SAXParseException ex) {
            throw new XmlBeanDefinitionStoreException(resource.getDescription(),
                    "Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
        }
        catch (SAXException ex) {
            throw new XmlBeanDefinitionStoreException(resource.getDescription(),
                    "XML document from " + resource + " is invalid", ex);
        }
        catch (ParserConfigurationException ex) {
            throw new BeanDefinitionStoreException(resource.getDescription(),
                    "Parser configuration exception parsing XML from " + resource, ex);
        }
        catch (IOException ex) {
            throw new BeanDefinitionStoreException(resource.getDescription(),
                    "IOException parsing XML document from " + resource, ex);
        }
        catch (Throwable ex) {
            throw new BeanDefinitionStoreException(resource.getDescription(),
                    "Unexpected exception parsing XML document from " + resource, ex);
        }
    }

/**
     * Actually load the specified document using the configured DocumentLoader.
     * 
     */
    protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {
        return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,
                getValidationModeForResource(resource), isNamespaceAware());
    }

從try塊中的代碼可以看出,spring使用documentLoader將資源轉換成了Document資源,spring使用的documentLoader為DefaultDocumentLoader,loadDocument方法定義在此類中:

/**
     * Load the {@link Document} at the supplied {@link InputSource} using the standard JAXP-configured
     * XML parser.
     */
    @Override
    public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
            ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {

        DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
        if (logger.isDebugEnabled()) {
            logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
        }
        DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
        return builder.parse(inputSource);
    }

從這裏不難看出,這就是我們非常熟悉的DOM解析xml了,可以想象spring是根據XSD文件規定的格式解析了xml文件的各節點及屬性。我們再來回頭看看registerBeanDefinitions(doc, resource)方法,

/**
     * Register the bean definitions contained in the given DOM document.
     * Called by {@code loadBeanDefinitions}.
     * 
     */
    public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
        BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
        int countBefore = getRegistry().getBeanDefinitionCount();
        documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
        return getRegistry().getBeanDefinitionCount() - countBefore;
    }

方法說明很明確的告訴我們,這個方法是註冊給定的DOM文檔中包含的bean定義。到這裏思路就很明確了,spring將包裝的輸入流解析成DOM文檔,然後將DOM中包含的bean定義信息註冊到IOC容器持有的Map<String,BeanDefinition>對象中。只要我們的IOC容器持有了bean定義,就能正確的生產bean實例。

通過閱讀源碼,我們分析了Spring IOC的實現原理。有些實現細節並沒有去深究,更重要的是去理解它的核心思想和實現思路。

Spring源碼:IOC原理解析(二)