2017-08-11 91 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.
相關問題