10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>The</strong>re is an order of precedence as to which method is used for unmarshalling:<br />

■ If a factory class is identified in the annotation, a corresponding factory method in that class<br />

must also be identified and that method will be used.<br />

■ If a factory method is identified in the annotation but no factory class is identified, the<br />

factory method must reside in the current class. <strong>The</strong> factory method is used even if there is a<br />

public zero arg constructor method present.<br />

■ If no factory method is identified in the annotation, the class must contain a public zero arg<br />

constructor method.<br />

In this example, a factory class provides zero arg factory methods for several classes. <strong>The</strong><br />

@XmlType annotation on class OrderContext references the factory class. <strong>The</strong> unmarshaller will<br />

use the identified factory method in this class.<br />

public class OrderFormsFactory {<br />

public OrderContext newOrderInstance() {<br />

return new OrderContext()<br />

}<br />

public PurchaseOrderType newPurchaseOrderType() {<br />

return new newPurchaseOrderType();<br />

}<br />

}<br />

@XmlType(name="oContext", factoryClass="OrderFormsFactory",<br />

factoryMethod="newOrderInstance")<br />

public class OrderContext {<br />

public OrderContext(){ ..... }<br />

}<br />

In this example, a factory method is defined in a class, which also contains a standard class<br />

construct. Because the factoryMethod value is defined and no factoryClass is defined, the<br />

factory method newOrderInstance is used during unmarshalling.<br />

@XmlType(name="oContext", factoryMethod="newOrderInstance")<br />

public class OrderContext {<br />

public OrderContext(){ ..... }<br />

public OrderContext newOrderInstance() {<br />

return new OrderContext();<br />

}<br />

}<br />

<strong>Java</strong>-to-Schema Examples<br />

Building and Running the XmlType Example Using NetBeans IDE<br />

Follow these instructions to build and run the XmlType example on your Application Server<br />

instance using NetBeans IDE.<br />

1. In NetBeans IDE, select File→Open Project.<br />

2. In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/jaxb/.<br />

Chapter 17 • Binding between XML Schema and <strong>Java</strong> Classes 545

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!