background image

Running the XmlType Example

<< Running the XmlSchemaType Class Example | More Information about JAXB >>
<< Running the XmlSchemaType Class Example | More Information about JAXB >>

Running the XmlType Example

There is an order of precedence as to which method is used for unmarshalling:
If a factory class is identified in the annotation, a corresponding factory method in that class
must also be identified and that method will be used.
If a factory method is identified in the annotation but no factory class is identified, the
factory method must reside in the current class. The factory method is used even if there is a
public zero arg constructor method present.
If no factory method is identified in the annotation, the class must contain a public zero arg
constructor method.
In this example, a factory class provides zero arg factory methods for several classes. The
@XmlType
annotation on class OrderContext references the factory class. The unmarshaller will
use the identified factory method in this class.
public class OrderFormsFactory {
public OrderContext newOrderInstance() {
return new OrderContext()
}
public PurchaseOrderType newPurchaseOrderType() {
return new newPurchaseOrderType();
}
}
@XmlType(name=
"oContext", factoryClass="OrderFormsFactory",
factoryMethod=
"newOrderInstance")
public class OrderContext {
public OrderContext(){ ..... }
}
In this example, a factory method is defined in a class, which also contains a standard class
construct. Because the factoryMethod value is defined and no factoryClass is defined, the
factory method newOrderInstance is used during unmarshalling.
@XmlType(name=
"oContext", factoryMethod="newOrderInstance")
public class OrderContext {
public OrderContext(){ ..... }
public OrderContext newOrderInstance() {
return new OrderContext();
}
}
Building and Running the XmlType Example Using NetBeans IDE
Follow these instructions to build and run the XmlType example on your Application Server
instance using the NetBeans IDE.
1. In NetBeans IDE, select File
Open Project.
2. In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/jaxb/.
Java-to-Schema Examples
Chapter 17 · Binding between XML Schema and Java Classes
547