16--Spring將Xml文件解析為Document對象

上一節(jié)分析了XmlBeanDefinitionReader以及系統(tǒng)環(huán)境的初始化门驾,本小節(jié)分析Spring解析xml的過程中的將Xml文件解析為Document對象登馒。

先來回顧一下Java解析xml的方式。包括DOM解析婴噩、SAX解析XML、JDOM解析XML、DOM4J解析XML等爬虱,每種解析方式各有優(yōu)缺點。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轉(zhuǎn)換為Document
    Document doc = docBuilder.parse(in);
    // 5稀并、獲取節(jié)點并循環(huán)輸出節(jié)點值
    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"

========測試方法結(jié)束=======

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

2. Spring將xml轉(zhuǎn)換為Document對象分析

打開XmlBeanFactory類

/**
 * 通過指定Resource對象和父BeanFactory創(chuàng)建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 {
    // 依次向上實例化父類構(gòu)造器
    super(parentBeanFactory);
    // 解析xml配置文件,將其轉(zhuǎn)換為IoC容器的內(nèi)部表示
    this.reader.loadBeanDefinitions(resource);
}

this.reader.loadBeanDefinitions(resource); 該代碼的作用就是解析xml配置文件,將其轉(zhuǎn)換為IoC容器的內(nèi)部表示碘举。我們先分析其第一步操作:解析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防止資源文件循環(huán)加載
    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轉(zhuǎn)換為Document對象并執(zhí)行BeanDefinition注冊
/**
 * 真正開始執(zhí)行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);
    }
}

通過上面代碼的分析,已經(jīng)接觸到了將xml文件轉(zhuǎn)換為Document的核心Document doc = doLoadDocument(inputSource, resource);蝙场,其實并沒有我們想象中那么神秘凌停,跟我們之前分析的DOM解析是一樣的。但是其中有一些細節(jié)還是值得我們?nèi)シ治龅摹?/p>

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

    // 1售滤、創(chuàng)建DocumentBuilderFactory對象
    DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
    // 2罚拟、創(chuàng)建DocumentBuilder對象
    DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
    // 3、將inputSource解析為Document對象
    return builder.parse(inputSource);
}

轉(zhuǎn)換過程一共分為了三步完箩,這與DOM解析的流程差不多赐俗,來具體分析一下其中的一些細節(jié)。


1.創(chuàng)建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.創(chuàng)建DocumentBuilder對象
protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory,
            @Nullable EntityResolver entityResolver, @Nullable ErrorHandler errorHandler)
            throws ParserConfigurationException {
    // 1秩彤、創(chuàng)建DocumentBuilder對象
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    // 2叔扼、嘗試設(shè)置entityResolver
    if (entityResolver != null) {
        docBuilder.setEntityResolver(entityResolver);
    }
    // 3、嘗試設(shè)置errorHandler
    if (errorHandler != null) {
        docBuilder.setErrorHandler(errorHandler);
    }
    return docBuilder;
}

這里有一個EntityResolver類漫雷,該類的作用是避免從網(wǎng)絡(luò)上尋找DTD聲明瓜富。至于轉(zhuǎn)換方法本節(jié)不在分析,因為涉及到了jdk的源碼珊拼,且不是我們分析的重點食呻。

總之Spring將Xml文件解析為Document對象的過程就是使用了Java的DOM解析,只不過在解析之上做了一些額外的操作澎现,例如防止文件重復加載仅胞、xml驗證模式、
設(shè)置EntityResolver剑辫、設(shè)置errorHandler等等干旧。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市妹蔽,隨后出現(xiàn)的幾起案子椎眯,更是在濱河造成了極大的恐慌挠将,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件编整,死亡現(xiàn)場離奇詭異舔稀,居然都是意外死亡,警方通過查閱死者的電腦和手機掌测,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門内贮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人汞斧,你說我怎么就攤上這事夜郁。” “怎么了粘勒?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵竞端,是天一觀的道長。 經(jīng)常有香客問我庙睡,道長事富,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任埃撵,我火速辦了婚禮旬盯,結(jié)果婚禮上行剂,老公的妹妹穿的比我還像新娘。我一直安慰自己嫩海,他們只是感情好捂刺,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布谣拣。 她就那樣靜靜地躺著,像睡著了一般族展。 火紅的嫁衣襯著肌膚如雪森缠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天仪缸,我揣著相機與錄音贵涵,去河邊找鬼。 笑死恰画,一個胖子當著我的面吹牛宾茂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播拴还,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼跨晴,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了片林?” 一聲冷哼從身側(cè)響起端盆,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤怀骤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后焕妙,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蒋伦,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年焚鹊,在試婚紗的時候發(fā)現(xiàn)自己被綠了凉敲。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡寺旺,死狀恐怖爷抓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情阻塑,我是刑警寧澤蓝撇,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站陈莽,受9級特大地震影響渤昌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜走搁,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一独柑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧私植,春花似錦忌栅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至贫悄,卻和暖如春瑞驱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背窄坦。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工唤反, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鸭津。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓彤侍,卻偏偏與公主長得像,于是被迫代替她去往敵國和親曙博。 傳聞我的和親對象是個殘疾皇子拥刻,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容