讓我們瞭解下DocumentBuilderFactory------------【spring源碼】

DocumentBuilderFactory,它的詳情在這張圖片上。。。

 

 

從上面我們瞭解到這個包是,提供允許處理XML文檔的類。而且我們分析的類是:定義工廠API,使應用程序能夠從XML文檔中獲取生成DOM對象樹的解析器。

 

如果不明白和應用的話,可以看下這篇文章:DocumentBuilderFactory解析XML

 

 

至於其中比較重要的方法:newInstance方法,它會根據本地平臺默認安裝的解析器,自動創建一個工廠的對象並返回。

但是在DocumentBuilderFactory類中newInstance方法有兩個,但它們方法名一樣,方法簽名不一樣的。

 

第一個,

    public static DocumentBuilderFactory newInstance() {
        return FactoryFinder.find(
                /* The default property name according to the JAXP spec */
                DocumentBuilderFactory.class, // "javax.xml.parsers.DocumentBuilderFactory"
                /* The fallback implementation class name */
                "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
    }

 

第二個,

    public static DocumentBuilderFactory newInstance(String factoryClassName, ClassLoader         
        classLoader){
            //do not fallback if given classloader can't find the class, throw exception
            return FactoryFinder.newInstance(DocumentBuilderFactory.class,
                        factoryClassName, classLoader, false);
    }

 

至於爲什麼的話,就是在有名字和類加載器的使用用自定義的,沒有的話,就是用默認的唄。其實一般都會這樣。。。

 

我們瞭解到這裏的話,也就差不多了。

至於它的用處的話,就是它會作用在哪裏,就對應着它的功能。

 

所以在spring源碼中,它用在了獲取 Document 對象的過程中。

貼下圖片,

 

詳細代碼:

至於自定義,我相信你能猜到的。。。

 

 

要是哪裏寫的地方不對,還望多多指教。

參考資料:

https://docs.oracle.com/javase/8/docs/api/

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