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 )
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.newInstance("com.package.one");
Unmarshaller jaxbUnmarshaller1 = jaxbContext.createUnmarshaller();
JAXBContext jaxbContext2 = JAXBContext.newInstance("com.package.two");
Unmarshaller jaxbUnmarshaller2 = jaxbContext2.createUnmarshaller();
EDIT when 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.
JAXBContext jaxbContext = JAXBContext.newInstance("com.package.one:com.package.two");
The rules for JAXB in a multi-threaded environment are very simple: you can share the JAXBContext object among threads. Doing so will also improve performance, as the construction of the context may be expensive. All other objects, including Marshaller and Unmarshaller, are not thread-safe and must not be shared.
The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate.
The JAXBContext. newInstance() method accepts jaxb. index files in the same way that it accepts the ObjectFactory class. Using a jaxb. index file requires you to evaluate all the instances in the trace file that indicate a package does not contain an ObjectFactory class, and create the appropriate jaxb.
From the Javadoc for JAXBContext:
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With