Java 如何指定特定的JAXB实现?

Java 如何指定特定的JAXB实现?,java,jakarta-ee,jboss,jaxb,Java,Jakarta Ee,Jboss,Jaxb,似乎我过去已经做过一次,但我找不到任何关于我做了什么让它工作的参考资料 我有一个web应用程序,我想在其中指定一个不同于web服务器/jre提供的JAXB实现。我从maven下载了相应的工件,并看到jar在我的war中得到了正确的打包,然而,当我启动我的web应用程序时,我看到它仍然在使用捆绑的JRE实现 我模模糊糊地记得一些关于属性文件的东西,我可以配置它,但找不到如何配置它的参考。此外,如果我想要使用的实现是相同的(只是更新的版本),那么类名将与打包在JRE中的类名相同。如何指定要使用WAR

似乎我过去已经做过一次,但我找不到任何关于我做了什么让它工作的参考资料

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

我模模糊糊地记得一些关于属性文件的东西,我可以配置它,但找不到如何配置它的参考。此外,如果我想要使用的实现是相同的(只是更新的版本),那么类名将与打包在JRE中的类名相同。如何指定要使用WAR中捆绑的产品

编辑

我目前运行的是JBoss7.0.2,带有OracleJDK1.6_0_27,是JRE附带的JAXBRI(我认为是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.
我觉得很奇怪。我不确定它在哪里找到了这些信息。更多的研究在MANIFEST.MF中发现了这一行:

Class-Path: jaxb-api.jar activation.jar jsr173_1.0_api.jar jaxb1-impl.jar
所以现在我比以前更困惑了。jaxb实现似乎依赖于api、激活、jsr和jaxb1实现JAR。但它们并没有列在jaxb pom中。我在网上进行了一些挖掘,讨论了如何在Java6SE环境中使用JAXB2.2。不幸的是,这似乎也不起作用;我仍然收到上述警告消息

我使用以下代码片段列出正在运行的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

注意:我是专家组的负责人和成员

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

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
了解更多信息


可以使用从特定库加载JAXB实现。指南中也提出了这一点


或者,在应用服务器中,您可以将应用程序的类加载器策略修改为parent last,以便在JRE中搜索之前,首先在与应用程序绑定的库中查找类。

这可能在某种程度上取决于JAXB实现。事实上,JAXB RI曾经要求在内存可用的情况下使用JRE“认可”机制。能否请您指定您的环境的详细信息:app server、JDK版本、JAXB impl form/to开头。@PatriceM-sorry;忘了包括那个。我编辑了原始问题,添加了它。我编辑了原始帖子,添加了一些额外的信息,说明我如何使用JAXB实现,以及一个链接,说明覆盖JRE的默认JAXB实现似乎不起作用;我想这就是我想要记住的。但是,如果JRE和war中都有相同的类,那么您如何指定在war中使用该类,而不是在JRE或提供应用程序服务器中使用该类?这是否有效?compile compile意味着您需要JAR来编译和运行应用程序。例如,对于web应用程序,JAR将被放置在web-INF/lib目录中。