本文介绍了为多个Java包指定MOXy运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将MOXy指定为我的JAXB实现,对于分布在多个Java包中的域类,除了放置?

Is there a way to specify MOXy as my JAXB implementation, for domain classes spread across multiple Java packages, other than putting jaxb.properties into every single package?

推荐答案

要指定作为您需要的JAXB提供商将jaxb.properties放在域对象的其中一个包中,即传入以引导JAXBContext。例如,如果您的JAXBContext将基于以下两个类:

To specify EclipseLink MOXy as the JAXB provider you need to put a jaxb.properties in one of the packages for your domain objects, that is passed in to bootstrap the JAXBContext. For example if your JAXBContext will be based on the following 2 classes:


  • example.foo.Foo

  • example.bar.Bar

example.foo.Foo

package example.foo;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

import example.bar.Bar;

@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    private Bar bar;

}

example.bar.Bar

package example.bar;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

import example.foo.Foo;

@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {

    private Foo foo;

}

example / foo / jaxb.properties

要指定应该使用JAXB的MOXy实现,我们将把一个带有以下条目的jaxb.properties文件放在与Foo类相同的包中。

To specify that the MOXy implementation of JAXB should be used we will put a jaxb.properties file with the following entry in the same package as the Foo class.

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

演示

由于Foo和Bar类相互引用,最终JAXBContext将包含关于它们的元数据,但根据我们如何创建JAXBContext,提供者可以是不同的。

Since the Foo and Bar classes reference each other, ultimately the JAXBContext will contain metadata about both of them, but based on how we create the JAXBContext the provider can be different.

package example;

import javax.xml.bind.JAXBContext;
import example.foo.Foo;
import example.bar.Bar;

public class Demo {

    public static void main(String[] args) throws Exception{
        System.out.println(JAXBContext.newInstance(Foo.class).getClass());
        System.out.println(JAXBContext.newInstance(Bar.class).getClass());
        System.out.println(JAXBContext.newInstance(Foo.class, Bar.class).getClass());
    }

}

运行上述代码将产生:

Running the above code will produce:

class org.eclipse.persistence.jaxb.JAXBContext
class com.sun.xml.bind.v2.runtime.JAXBContextImpl
class org.eclipse.persistence.jaxb.JAXBContext

这篇关于为多个Java包指定MOXy运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:06