如何指定特定的 JAXB 实现? [英] How to specify a particular JAXB implementation?

查看:46
本文介绍了如何指定特定的 JAXB 实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像我过去已经这样做过一次,但我找不到任何关于我为使其工作所做的工作的参考.

It seems as though I have already done this once in the past, but I cannot find any reference to what I had done to get it to work.

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

I have a web application in which I want to specify a different JAXB implementation than the one provided by my web server/jre. I downloaded the appropriate artifact from maven, and see that the jar is properly packaged in my war, however, when I start my web app, I see that it is still using the bundled JRE implementation.

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

I vaguely remember something about a properties file that I could configure, but cannot find a reference to how it needs to be configured. Moreover, if the implementation I want to use is the same one (just a newer version), the class names would be the same as though packaged in the JRE. How can I specify that I want to use the ones bundled in my WAR?

编辑

我目前在 JBoss 7.0.2 上运行,使用 Oracle JDK 1.6_0_27,JRE 附带的 JAXB RI(我认为它是 v2.1).我正在尝试升级到 JAXB RI 2.2.5(在 MvnRepository 上找到).

I'm currently running on JBoss 7.0.2, with Oracle JDK 1.6_0_27, JAXB RI that comes with the JRE (I think it is v2.1). I'm trying to upgrade to JAXB RI 2.2.5 (found on MvnRepository).

今天早上我做了更多的挖掘,并注意到我的日志中有一条奇怪的错误消息:

I've done a little more digging this morning, and noticed a strange error message in my logs:

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 中发现了这一行:

Which I found very strange. I wasn't sure where it was finding that information. A little more research found this line in the 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 环境中使用 JAXB 2.2.不幸的是,这似乎也不起作用.我仍然收到上述警告消息.

So now I'm even more confused than ever. It would appear that the jaxb implementation depends on the api, activation, jsr and jaxb1 implementation jars. But they aren't listed in the jaxb pom. A little digging online found me this link which discusses how to use JAXB 2.2 in a Java6SE environment. Unfortunately, this has not seemed to work either; I still get the above WARN messages.

我正在使用以下代码段列出正在运行的 JAXB 实现;也许这是不正确的?

I'm using the following snippet to list the JAXB implementation that is running; perhaps this is incorrect?

/**
 * 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());
}

此代码段生成以下日志:

This snippet produces the following log:

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

推荐答案

注意:我是EclipseLink JAXB (MOXy) 领导和成员 JAXB 2 (JSR-222) 专家组.

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

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

To specify a JAXB (JSR-222) implementation other than the default you need to include a file called jaxb.properties in the same package as your domain classes. The following is an example of the jaxb.properties file used to specify the MOXy implementation of JAXB:

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

了解更多信息

这篇关于如何指定特定的 JAXB 实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆