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 对象的过程中。
贴下图片,
详细代码:
至于自定义,我相信你能猜到的。。。
要是哪里写的地方不对,还望多多指教。
参考资料: