如何指定特定的JAXB实现?

Eri*_* B. 21 java jboss jaxb java-ee

好像我过去曾经做过一次这样的事情,但是我找不到任何关于我做了什么才能让它发挥作用的参考.

我有一个Web应用程序,我想在其中指定一个不同于我的web服务器/ jre提供的JAXB实现.我从maven下载了相应的工件,看到jar在我的战争中正确打包,但是,当我启动我的Web应用程序时,我发现它仍在使用捆绑的JRE实现.

我依旧记得有关我可以配置的属性文件的一些内容,但是找不到它需要如何配置的引用.此外,如果我想要使用的实现是相同的(只是一个较新的版本),类名将与在JRE中打包相同.如何指定我想使用WAR中捆绑的那些?

编辑

我目前正在运行JBoss 7.0.2,使用Oracle JDK 1.6_0_27,JRE附带的JAXB RI(我认为它是v2.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.
Run Code Online (Sandbox Code Playgroud)

我觉得很奇怪.我不确定它在哪里找到这些信息.稍微研究一下在MANIFEST.MF中发现了这一行:

Class-Path: jaxb-api.jar activation.jar jsr173_1.0_api.jar jaxb1-impl.jar
Run Code Online (Sandbox Code Playgroud)

所以现在我比以前更加困惑.似乎jaxb实现依赖于api,activation,jsr和jaxb1实现jar.但它们没有列在jaxb pom中.在线挖掘了一下这个链接,它讨论了如何在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());
}
Run Code Online (Sandbox Code Playgroud)

此代码段生成以下日志:

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
Run Code Online (Sandbox Code Playgroud)

bdo*_*han 13

注意: 我是EclipseLink JAXB(MOXy)的负责人,也是JAXB 2(JSR-222)专家组的成员.

要指定除默认值之外的JAXB(JSR-222)实现,您需要jaxb.properties在与域类相同的包中包含一个调用的文件.以下是jaxb.properties用于指定JAXB的MOXy实现的文件示例:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Run Code Online (Sandbox Code Playgroud)

欲获得更多信息

  • 谢谢你的提示; 我想那就是我想要记住的东西.但是在JRE和你的战争中给出相同的类,你如何指定在战争中使用类与jre或提供应用服务器中的类? (4认同)