java 多个 JAXBContext 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13399567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple JAXBContext instances
提问by Spring
By using XJC, I create 2 different JAXB metadata packages with an ObjectFactory class in each package (I dont know if this approach is OK, I have 2 different XSD's to work on )
通过使用 XJC,我创建了 2 个不同的 JAXB 元数据包,每个包中都有一个 ObjectFactory 类(我不知道这种方法是否可行,我有 2 个不同的 XSD 可以处理)
It is recommended to create only one JAXBContext per operation, because it is costy. So I wonder if what I'm doing here is valid and good practise?
建议每个操作只创建一个 JAXBContext,因为它很昂贵。所以我想知道我在这里所做的是否有效和良好的做法?
JAXBContext jaxbContext = JAXBContext.newInstance("com.package.one");
Unmarshaller jaxbUnmarshaller1 = jaxbContext.createUnmarshaller();
JAXBContext jaxbContext2 = JAXBContext.newInstance("com.package.two");
Unmarshaller jaxbUnmarshaller2 = jaxbContext2.createUnmarshaller();
EDITwhen i try to initialize 2 packages together i get an exception "The element name {}Value has more than one mapping." Value is a class in both packages.
编辑当我尝试一起初始化 2 个包时,出现异常“元素名称 {}Value 有多个映射。” Value 是两个包中的一个类。
JAXBContext jaxbContext = JAXBContext.newInstance("com.package.one:com.package.two");
回答by Perception
From the Javadoc for JAXBContext:
来自 JAXBContext 的 Javadoc:
A client application normally obtains new instances of this class using one of these two styles for newInstance methods, although there are other specialized forms of the method available: JAXBContext.newInstance( "com.acme.foo:com.acme.bar" ) The JAXBContext instance is initialized from a list of colon separated Java package names. Each java package contains JAXB mapped classes, schema-derived classes and/or user annotated classes. Additionally, the java package may contain JAXB package annotations that must be processed. (see JLS 3rd Edition, Section 7.4.1. Package Annotations). JAXBContext.newInstance( com.acme.foo.Foo.class ) The JAXBContext instance is intialized with class(es) passed as parameter(s) and classes that are statically reachable from these class(es). See newInstance(Class...) for details.
You can use a shared context and initialize it with a list of package names.
您可以使用共享上下文并使用包名称列表对其进行初始化。
Code Example:
代码示例:
package test.jaxb.one;
@XMLRootElement
@XMLType(name = "test.jaxb.one.SimpleObject")
@XMLAccessorType(XMLAccessType.FIELD)
public class SimpleObject implements Serializable {
private static final long serialVersionUID = 54536613717262557148L;
@XmlElement(name = "Name")
private String name;
// Constructor, Setters/Getters
}
and this one:
还有这个:
package test.jaxb.two;
@XMLRootElement
@XMLType(name = "test.jaxb.two.SimpleObject")
@XMLAccessorType(XMLAccessType.FIELD)
public class SimpleObject implements Serializable {
private static final long serialVersionUID = -4073071224211934153L;
@XmlElement(name = "Name")
private String name;
// Constructor, Setters/Getters
}
Finally:
最后:
public class JAXBTest {
@Test
public void testContextLoad() throws Exception {
final JAXBContext context = JAXBContext
.newInstance("test.jaxb.one:test.jaxb.two");
Assert.assertNotNull(context);
}
}