2017-08-11 41 views
0

我从我的Tibco进程调用java类的方法。这个类使用DocumentBuilderFactroy抽象类。当我在eclipse本地运行应用程序选择工厂类的提供者

DocumentBuilderFactroy documentBuilderFactroy = DocumentBuilderFactroy.neInstance(); 

,一切工作正常。但在部署模式,我有以下错误:

Provider for javax.xml.parsers.DocumentBuilderFactory can not be created.

因此,我改变指定的实现类实例中的代码documentBuilderFactroy对象的方式。

String providerDBF = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"; 
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance (providerDBF, null); 

我的问题是:我如何指定实现类来避免此问题没有硬编码呢?

我没有使用Maven。

谢谢。

回答

0

您可以在JRE目录使用javax.xml.parsers.DocumentBuilderFactory system propertyproperties file

DocumentBuilderFactory documentation

public static DocumentBuilderFactory newInstance() 

Obtain a new instance of a DocumentBuilderFactory. This static method creates a new factory instance. This method uses the following ordered lookup procedure to determine the DocumentBuilderFactory implementation class to load:

  • 使用javax.xml.parsers.DocumentBuilderFactory中的系统属性。
  • 在JRE目录中使用属性文件“lib/jaxp.properties”。 此配置文件采用标准java.util.Properties格式 ,并包含实现类 的全限定名,其中的关键字是上面定义的系统属性。 jaxp.properties文件仅由JAXP实现读取一次,然后 将其值缓存供将来使用。如果第一次尝试读取文件时文件不存在 ,则不会再尝试检查其是否存在。 不可能在第一次读取 之后更改jaxp.properties中的任何属性的值。
  • 使用Services API(详见JAR规范),如果有 可用,则确定类名。 Services API将在运行时可用的罐子 中的文件 META-INF/services/javax.xml.parsers.DocumentBuilderFactory中寻找 类名。
  • 平台默认DocumentBuilderFactory实例。一旦应用程序 获得对DocumentBuilderFactory的引用,它可以使用 工厂来配置和获取解析器实例。
0

JavaDocs状态,其中可以将其设置为:

This method uses the following ordered lookup procedure to determine the DocumentBuilderFactory implementation class to load:

  • Use the javax.xml.parsers.DocumentBuilderFactory system property.
  • Use the properties file "lib/jaxp.properties" in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to check for its existence. It is not possible to change the value of any property in jaxp.properties after it has been read for the first time.
  • Uses the service-provider loading facilities, defined by the ServiceLoader class, to attempt to locate and load an implementation of the service using the default loading mechanism: the service-provider loading facility will use the current thread's context class loader to attempt to load the service. If the context class loader is null, the system class loader will be used.
  • Otherwise, the system-default implementation is returned.