栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何找出正在使用的JAXP实现以及从何处加载?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何找出正在使用的JAXP实现以及从何处加载?

在没有实际创建实例的情况下,很难预测将要加载哪些具体的JAXP工厂实现,因为选择实现的过程非常困难。

从官方JAXP常见问题解答(问题14)中:

当应用程序想要创建一个新的JAXP

documentBuilderFactory
实例时,它将调用staic方法
documentBuilderFactory.newInstance()
。这将导致
documentBuilderFactory
使用以下顺序搜索具体子类的名称


  1. 系统属性的值,例如
    javax.xml.parsers.documentBuilderFactory
    它是否存在并且可以访问。
  2. 文件的内容(
    $JAVA_HOME/jre/lib/jaxp.properties
    如果存在)。
  3. Jar文件规范中指定的Jar服务提供者发现机制。jar文件可以具有一个资源(即嵌入式文件),例如
    meta-INF/services/javax.xml.parsers.documentBuilderFactory
    包含要实例化的具体类的名称。
  4. 后备平台的默认实现。

更复杂的是,每个JAXP工厂都可以指定一个独立的实现。通常使用一个解析器实现和另一个XSLT实现,但是上面选择机制的粒度使您可以更大程度地进行混合和匹配。

以下代码将输出有关四个主要JAXP工厂的信息:

private static void OutputJaxpImplementationInfo() {    System.out.println(getJaxpImplementationInfo("documentBuilderFactory", documentBuilderFactory.newInstance().getClass()));    System.out.println(getJaxpImplementationInfo("XPathFactory", XPathFactory.newInstance().getClass()));    System.out.println(getJaxpImplementationInfo("TransformerFactory", TransformerFactory.newInstance().getClass()));    System.out.println(getJaxpImplementationInfo("SAXParserFactory", SAXParserFactory.newInstance().getClass()));}private static String getJaxpImplementationInfo(String componentName, Class componentClass) {    CodeSource source = componentClass.getProtectionDomain().getCodeSource();    return MessageFormat.format( "{0} implementation: {1} loaded from: {2}", componentName, componentClass.getName(), source == null ? "Java Runtime" : source.getLocation());}

以下样本输出 说明了三种不同的JAXP实现(内置Xerces和Xerces 2.8和Xalan的外部JAR)的混合搭配:

documentBuilderFactory implementation: org.apache.xerces.jaxp.documentBuilderFactoryImpl loaded from: file:/C:/Projects/Scratch/lib/xerces-2.8.0.jarXPathFactory implementation: com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl loaded from: Java RuntimeTransformerFactory implementation: org.apache.xalan.processor.TransformerFactoryImpl loaded from: file:/C:/Projects/Scratch/lib/xalan.jarSAXParserFactory implementation: org.apache.xerces.jaxp.SAXParserFactoryImpl loaded from: file:/C:/Projects/Scratch/lib/xerces-2.8.0.jar


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486833.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号