2012-04-03 77 views
17

看起來好像我已經在過去做過這樣的事情了,但是我找不到任何我曾經做過的工作來完成它。如何指定特定的JAXB實現?

我有一個web應用程序,我想在其中指定一個不同於我的web服務器/ jre提供的JAXB實現。我從maven下載了相應的工件,並且看到jar在我的戰爭中被正確打包,但是,當我啓動我的web應用程序時,我發現它仍在使用捆綁的JRE實現。

我依稀記得有關可以配置的屬性文件的一些信息,但無法找到需要配置的屬性文件的參考。而且,如果我想要使用的實現是同一個(只是更新的版本),那麼類名將與JRE中打包的相同。我如何指定我想要使用WAR中捆綁的那些?

編輯

我目前在JBoss 7.0.2運行,與Oracle JDK 1.6_0_27,JAXB RI自帶的JRE(我認爲這是2.1版)。我試圖升級到JAXB RI 2.2.5(在MvnRepository上找到)。

我做多一點挖今天上午,在我的日誌中發現一個奇怪的錯誤消息:

09:43:18,325 WARN [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry jaxb-api.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar" does not point to a valid jar for a Class-Path reference. 
09:43:18,325 WARN [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry activation.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar" does not point to a valid jar for a Class-Path reference. 
09:43:18,326 WARN [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry jsr173_1.0_api.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar" does not point to a valid jar for a Class-Path reference. 
09:43:18,326 WARN [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry jaxb1-impl.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar" does not point to a valid jar for a Class-Path reference. 

我發現很奇怪。我不確定它在哪裏找到這些信息。多一點研究發現MANIFEST.MF中的這條線:

Class-Path: jaxb-api.jar activation.jar jsr173_1.0_api.jar jaxb1-impl.jar 

所以現在我比以前更困惑了。看來jaxb實現依賴於api,activation,jsr和jaxb1實現jar。但他們沒有在jaxb pom中列出。有一點在網上發現我找到了this link,它討論瞭如何在Java6SE環境中使用JAXB 2.2。不幸的是,這似乎也沒有工作;我仍然收到上面的WARN消息。

我正在使用下面的代碼片段列出正在運行的JAXB實現;也許這是不正確的?

/** 
* Print the JAXB Implementation information 
*/ 
public static void outputJaxpImplementationInfo() { 
    logger.debug(getImplementationInfo("DocumentBuilderFactory", DocumentBuilderFactory.newInstance().getClass())); 
    logger.debug(getImplementationInfo("XPathFactory", XPathFactory.newInstance().getClass())); 
    logger.debug(getImplementationInfo("TransformerFactory", TransformerFactory.newInstance().getClass())); 
    logger.debug(getImplementationInfo("SAXParserFactory", SAXParserFactory.newInstance().getClass())); 
} 

/** 
* Get the JAXB implementation information for a particular class 
* @param componentName 
* @param componentClass 
* @return 
*/ 
private static String getImplementationInfo(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()); 
} 

這段代碼生成以下日誌:

10:28:27,402 INFO [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,402 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil - DocumentBuilderFactory implementation: __redirected.__DocumentBuilderFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar 
10:28:27,403 INFO [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,403 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil - XPathFactory implementation: __redirected.__XPathFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar 
10:28:27,404 INFO [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,404 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil - TransformerFactory implementation: __redirected.__TransformerFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar 
10:28:27,406 INFO [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,406 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil - SAXParserFactory implementation: __redirected.__SAXParserFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar 
+1

這可能是JAXB實現依賴於某種程度。例如,如果內存需要使用JAXB RI,則需要使用JRE'認可'機制。您可以請指定您的環境的詳細信息:應用程序服務器,JDK版本,JAXB impl表單/到開始。 – 2012-04-03 23:00:30

+0

@PatriceM - 抱歉;忘了包括那個。剛纔編輯的原稿問題補充進去。 – 2012-04-04 03:02:36

+0

我編輯了原來的職位,以添加一些額外的信息,指示我如何讓JAXB實現中使用,以及指示重寫JRE的默認JAXB實現似乎都不怎麼做個鏈接加工。 – 2012-04-04 14:37:33

回答

12

注:我是EclipseLink JAXB (MOXy)鉛和JAXB 2 (JSR-222)專家小組的成員。

要指定默認以外的JAXB(JSR-222)實現,您需要在與您的域類相同的包中包含一個名爲jaxb.properties的文件。以下是用於指定MOXY實施JAXB的jaxb.properties文件的一個例子:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

更多信息

+4

感謝您的提示;我想那就是我想要記住的。但鑑於既有JRE並在戰爭中同一個班級,你如何指定要使用的類在戰爭VS那個在JRE或提供應用服務器? – 2012-04-04 03:04:24

+0

會這樣工作嗎? 編譯 編譯意味着你需要編譯和運行應用程序的JAR。例如,對於Web應用程序,JAR將被放置在WEB-INF/lib目錄中。 – aurelius 2015-01-21 07:52:19