gpt4 book ai didi

java - 如何指定特定的 JAXB 实现?

转载 作者:IT老高 更新时间:2023-10-28 20:47:40 26 4
gpt4 key购买 nike

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

我有一个 Web 应用程序,我想在其中指定与我的 Web 服务器/jre 提供的 JAXB 实现不同的 JAXB 实现。我从 maven 下载了适当的工件,并看到该 jar 已正确打包在我的 war 中,但是,当我启动我的 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.

我觉得很奇怪。我不确定它在哪里找到这些信息。更多研究在 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。不幸的是,这似乎也不起作用。我仍然收到上述警告消息。

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

最佳答案

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

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

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

更多信息

关于java - 如何指定特定的 JAXB 实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10002006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com