天天看點

16--Spring将Xml檔案解析為Document對象

上一節分析了XmlBeanDefinitionReader以及系統環境的初始化,本小節分析Spring解析xml的過程中的将Xml檔案解析為Document對象。

先來回顧一下Java解析xml的方式。包括DOM解析、SAX解析XML、JDOM解析XML、DOM4J解析XML等,每種解析方式各有優缺點。Spring使用的是第一種解析方式DOM解析,先通過一個例子來看一下Java是如何将xml檔案解析為Document對象的。這将有助于接下來對Spring源碼的分析。

1. Java DOM解析xml檔案
  • DOM解析
@Test
public void test14() throws ParserConfigurationException, IOException, SAXException {
    // 解析xml檔案
    // 1、擷取InputStream輸入流
    InputStream in = new ClassPathResource("v2/day01.xml").getInputStream();
    // 2、擷取DocumentBuilderFactory執行個體
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // 3、擷取DocumentBuilder執行個體
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    // 4、将docBuilder轉換為Document
    Document doc = docBuilder.parse(in);
    // 5、擷取節點并循環輸出節點值
    Element element = doc.getDocumentElement();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        //System.out.println(node.getNodeName());
        NamedNodeMap attributes = node.getAttributes();
        if (null != attributes) {
            System.out.println(attributes.getNamedItem("id"));
            System.out.println(attributes.getNamedItem("class"));
        }
    }
}
           
  • 輸出
========測試方法開始=======

id="dog1"
class="com.lyc.cn.v2.day01.Dog"
id="dog2"
class="com.lyc.cn.v2.day01.Dog"
id="dog3"
class="com.lyc.cn.v2.day01.DogStaticFactory"
id="dogFactory"
class="com.lyc.cn.v2.day01.DogFactory"
id="dog4"
null
id="outer"
class="com.lyc.cn.v2.day01.inner.Outer"
id="father"
class="com.lyc.cn.v2.day01.parent.Father"
id="sun"
class="com.lyc.cn.v2.day01.parent.Sun"
id="cat"
class="com.lyc.cn.v2.day01.collection.Cat"
id="car"
class="com.lyc.cn.v2.day01.method.lookupMethod.Car"
id="taxi"
class="com.lyc.cn.v2.day01.method.lookupMethod.Taxi"
id="dogReplaceMethod"
class="com.lyc.cn.v2.day01.method.replaceMethod.ReplaceDog"
id="originalDogReplaceMethod"
class="com.lyc.cn.v2.day01.method.replaceMethod.OriginalDog"
id="student"
class="com.lyc.cn.v2.day01.factoryBean.StudentFactoryBean"
id="furniture"
class="com.lyc.cn.v2.day01.factoryBean.FurnitureFactoryBean"
id="myLifeCycleBean"
class="com.lyc.cn.v2.day01.lifecycle.LifeCycleBean"
id="myBeanPostProcessor"
class="com.lyc.cn.v2.day01.lifecycle.LifeCycleBeanPostProcessor"
id="dog"
class="com.lyc.cn.v2.day01.Dog"
id="myBeanFactoryPostProcessor"
class="com.lyc.cn.v2.day01.lifecycle.MyBeanFactoryPostProcessor"

========測試方法結束=======
           

非常簡單,不再做過的分析。

2. Spring将xml轉換為Document對象分析

打開XmlBeanFactory類

/**
 * 通過指定Resource對象和父BeanFactory建立XmlBeanFactory執行個體
 * Create a new XmlBeanFactory with the given input stream,
 * which must be parsable using DOM.
 * @param resource          the XML resource to load bean definitions from
 * @param parentBeanFactory parent bean factory
 * @throws BeansException in case of loading or parsing errors
 */
public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
    // 依次向上執行個體化父類構造器
    super(parentBeanFactory);
    // 解析xml配置檔案,将其轉換為IoC容器的内部表示
    this.reader.loadBeanDefinitions(resource);
}
           

this.reader.loadBeanDefinitions(resource);

該代碼的作用就是解析xml配置檔案,将其轉換為IoC容器的内部表示。我們先分析其第一步操作:解析xml配置檔案。

跟蹤代碼,依次打開

  • 方法入口
/**
 * 加載BeanDefinition
 * Load bean definitions from the specified XML file.
 * @param resource the resource descriptor for the XML file
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
    return loadBeanDefinitions(new EncodedResource(resource));
}
           
  • 擷取InputStream對象
/**
 * 加載BeanDefinition
 * Load bean definitions from the specified XML file.
 * @param encodedResource the resource descriptor for the XML file,
 * allowing to specify an encoding to use for parsing the file
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
    // 1、使用ThreadLocal防止資源檔案循環加載
    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 {
        // 2、加載BeanDefinition
        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轉換為Document對象并執行BeanDefinition注冊
/**
 * 真正開始執行BeanDefinition的注冊
 * Actually load bean definitions from the specified XML file.
 * @param inputSource the SAX InputSource to read from
 * @param resource the resource descriptor for the XML file
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 * @see #doLoadDocument
 * @see #registerBeanDefinitions
 */
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException {
    try {
        // 資源檔案解析為Document對象
        Document doc = doLoadDocument(inputSource, resource);
        // 注冊BeanDefinitions
        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);
    }
}
           

通過上面代碼的分析,已經接觸到了将xml檔案轉換為Document的核心

Document doc = doLoadDocument(inputSource, resource);

,其實并沒有我們想象中那麼神秘,跟我們之前分析的DOM解析是一樣的。但是其中有一些細節還是值得我們去分析的。

  • 執行轉換
@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
        ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {

    // 1、建立DocumentBuilderFactory對象
    DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
    // 2、建立DocumentBuilder對象
    DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
    // 3、将inputSource解析為Document對象
    return builder.parse(inputSource);
}
           

轉換過程一共分為了三步,這與DOM解析的流程差不多,來具體分析一下其中的一些細節。

1.建立DocumentBuilderFactory對象

protected DocumentBuilderFactory createDocumentBuilderFactory(int validationMode, boolean namespaceAware)
			throws ParserConfigurationException {

    // 1、擷取DocumentBuilderFactory執行個體
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(namespaceAware);

    // 2、如果開啟xml驗證的話,則驗證xml
    if (validationMode != XmlValidationModeDetector.VALIDATION_NONE) {
        factory.setValidating(true);
        // 如果xml驗證模式為XSD則需要強制指定由此代碼生成的解析器将提供對XML名稱空間的支援
        if (validationMode == XmlValidationModeDetector.VALIDATION_XSD) {
            // Enforce namespace aware for XSD...
            factory.setNamespaceAware(true);
            try {
                factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE);
            }
            catch (IllegalArgumentException ex) {
                ParserConfigurationException pcex = new ParserConfigurationException(
                        "Unable to validate using XSD: Your JAXP provider [" + factory +
                        "] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? " +
                        "Upgrade to Apache Xerces (or Java 1.5) for full XSD support.");
                pcex.initCause(ex);
                throw pcex;
            }
        }
    }

    return factory;
}
           
  • 2.建立DocumentBuilder對象
protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory,
			@Nullable EntityResolver entityResolver, @Nullable ErrorHandler errorHandler)
			throws ParserConfigurationException {
    // 1、建立DocumentBuilder對象
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    // 2、嘗試設定entityResolver
    if (entityResolver != null) {
        docBuilder.setEntityResolver(entityResolver);
    }
    // 3、嘗試設定errorHandler
    if (errorHandler != null) {
        docBuilder.setErrorHandler(errorHandler);
    }
    return docBuilder;
}
           

這裡有一個EntityResolver類,該類的作用是避免從網絡上尋找DTD聲明。至于轉換方法本節不在分析,因為涉及到了jdk的源碼,且不是我們分析的重點。

總之Spring将Xml檔案解析為Document對象的過程就是使用了Java的DOM解析,隻不過在解析之上做了一些額外的操作,例如防止檔案重複加載、xml驗證模式、

設定EntityResolver、設定errorHandler等等。