如何指定特定的JAXB实现?

时间:2022-11-16 10:47:57

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.

好像我以前已经做过一次,但是我找不到任何关于我为使它工作所做的事情的参考。

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.

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

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?

我模糊地记得关于可以配置的属性文件的一些内容,但是找不到需要如何配置它的引用。此外,如果我想要使用的实现是相同的(只是一个更新的版本),那么类名将与在JRE中打包的相同。我怎么能指定我想要在我的战争中使用捆绑的?

EDIT

编辑

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).

我目前正在运行JBoss 7.0.2,使用Oracle JDK 1.6_0_27和JRE附带的JAXB RI(我认为是v2.1)。我正在尝试升级到JAXB RI 2.2.5(在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.

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

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实现似乎依赖于api、激活、jsr和jaxb1实现jar。但是它们不在jaxb pom中列出。在网上的一个小挖掘发现了这个链接,它讨论了如何在Java6SE环境中使用JAXB 2.2。不幸的是,这似乎也没有起作用;我仍然收到上面的警告信息。

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

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

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

2 个解决方案

#1


13  

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

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

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:

要指定JAXB (JSR-222)实现,需要包含一个名为JAXB的文件。属性在与域类相同的包中。下面是jaxb的一个示例。用于指定JAXB的MOXy实现的属性文件:

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

For More Information

的更多信息

#2


2  

It is possible to load JAXB implementation from a specific library using the Java Endorsed Standards Override Mechanism. This is also suggested in the guide Using JAXB 2.1/2.2 with JavaSE 6.

可以使用Java认可的标准覆盖机制从特定库加载JAXB实现。在使用JAXB 2.1/2.2和JavaSE 6的指南中也建议这样做。

Alternatively, in your application server you may modify the class-loader policy for your application to parent last so that classes are first looked up in the libraries bundled with your application before being searched in JRE.

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

#1


13  

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

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

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:

要指定JAXB (JSR-222)实现,需要包含一个名为JAXB的文件。属性在与域类相同的包中。下面是jaxb的一个示例。用于指定JAXB的MOXy实现的属性文件:

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

For More Information

的更多信息

#2


2  

It is possible to load JAXB implementation from a specific library using the Java Endorsed Standards Override Mechanism. This is also suggested in the guide Using JAXB 2.1/2.2 with JavaSE 6.

可以使用Java认可的标准覆盖机制从特定库加载JAXB实现。在使用JAXB 2.1/2.2和JavaSE 6的指南中也建议这样做。

Alternatively, in your application server you may modify the class-loader policy for your application to parent last so that classes are first looked up in the libraries bundled with your application before being searched in JRE.

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