title: @XmlRegistry - how does it work?
body: <p>I have found some examples of JAXB2 <code>@XmlRegistry</code> over the internet but no good in-depth tutorials that talk about the concept of using <code>@XmlRegistry</code> with <code>@XmlElementDecl</code>, wonder if its a concept not much explored in general.</p>
  
  <p>Anyways here is my question, first some sample classes that I am using to unmarshall an xml using JAXB:</p>
  
  <p>The main class I am trying to unmarshal using JAXB - Employee.java</p>
  
  <pre><code>package com.test.jaxb;
  
  import java.util.List;
  
  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.annotation.XmlElementDecl;
  import javax.xml.bind.annotation.XmlRegistry;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.namespace.QName;
  
  import com.test.jaxb.dto.Address;
  
  @XmlRootElement
  public class Employee {
      private int id;
      private String name;
      private String email;
  
      private List&lt;Address&gt; addresses;
  
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public String getEmail() {
          return email;
      }
      public void setEmail(String email) {
          this.email = email;
      }
  
      public List&lt;Address&gt; getAddresses() {
          return addresses;
      }
      public void setAddresses(List&lt;Address&gt; addresses) {
          this.addresses = addresses;
      }
  
      @SuppressWarnings("unused")
      @XmlRegistry
      public static class XMLObjectFactory {
          @XmlElementDecl(scope = Employee.class, name= "id")
          JAXBElement&lt;String&gt; createEmployeeId(String value) {
              return new JAXBElement&lt;String&gt;(new QName("id"), String.class, "100");
          }
          @XmlElementDecl(scope = Employee.class, name= "name")
          JAXBElement&lt;String&gt; createName(String value) {
              return new JAXBElement&lt;String&gt;(new QName("name"), String.class, "Fake Name");
          }
          @XmlElementDecl(scope = Employee.class, name= "email")
          JAXBElement&lt;String&gt; createEmail(String value) {
              return new JAXBElement&lt;String&gt;(new QName("email"), String.class, value);
          }
  
          @XmlElementDecl(scope = Employee.class, name= "addresses")
          JAXBElement&lt;List&gt; createAddresses(List value) {
              return new JAXBElement&lt;List&gt;(new QName("addresses"), List.class, value);
          }
      }
  }
  </code></pre>
  
  <p>The child class - Address.java</p>
  
  <pre><code>package com.test.jaxb.dto;
  
  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.annotation.XmlElementDecl;
  import javax.xml.bind.annotation.XmlRegistry;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.namespace.QName;
  
  import com.test.jaxb.Employee;
  
  @XmlRootElement
  public class Address {
      private String addressLine1;
      private String addressLine2;
      private String addressLine3;
      public String getAddressLine1() {
          return addressLine1;
      }
      public void setAddressLine1(String addressLine1) {
          this.addressLine1 = addressLine1;
      }
      public String getAddressLine2() {
          return addressLine2;
      }
      public void setAddressLine2(String addressLine2) {
          this.addressLine2 = addressLine2;
      }
      public String getAddressLine3() {
          return addressLine3;
      }
      public void setAddressLine3(String addressLine3) {
          this.addressLine3 = addressLine3;
      }
  
      @SuppressWarnings("unused")
      @XmlRegistry
      private static class XMLObjectFactory {
          @XmlElementDecl(scope = Employee.class, name= "addressLine1")
          JAXBElement&lt;String&gt; createAddressLine1(String value) {
              return new JAXBElement&lt;String&gt;(new QName("addressLine1"), String.class, value);
          }
          @XmlElementDecl(scope = Employee.class, name= "addressLine2")
          JAXBElement&lt;String&gt; createAddressLine2(String value) {
              return new JAXBElement&lt;String&gt;(new QName("addressLine2"), String.class, value);
          }
          @XmlElementDecl(scope = Employee.class, name= "addressLine3")
          JAXBElement&lt;String&gt; createAddressLine3(String value) {
              return new JAXBElement&lt;String&gt;(new QName("addressLine3"), String.class, value);
          }
      }
  }
  </code></pre>
  
  <p>The xml to be unmarshalled - employee.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;employee&gt;
      &lt;id&gt;1&lt;/id&gt;
      &lt;name&gt;Vaishali&lt;/name&gt;
      &lt;email&gt;Vaishali@example.com&lt;/email&gt;
      &lt;addresses&gt;
          &lt;address&gt;
              &lt;addressLine1&gt;300&lt;/addressLine1&gt;
              &lt;addressLine2&gt;Mumbai&lt;/addressLine2&gt;
              &lt;addressLine3&gt;India&lt;/addressLine3&gt;
          &lt;/address&gt;
          &lt;address&gt;
              &lt;addressLine1&gt;301&lt;/addressLine1&gt;
              &lt;addressLine2&gt;Pune&lt;/addressLine2&gt;
              &lt;addressLine3&gt;India&lt;/addressLine3&gt;
          &lt;/address&gt;
      &lt;/addresses&gt;
  &lt;/employee&gt;
  </code></pre>
  
  <p>Unmarshalling Code : </p>
  
  <pre><code>package com.test.jaxb;
  
  import java.io.FileReader;
  
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.Unmarshaller;
  
  
  public class ObjectFactoryTest {
      public static void main(String[] args) throws Exception {
          FileReader reader = new FileReader("resources/employee.xml");
          JAXBContext context = JAXBContext.newInstance(Employee.class);
          Unmarshaller unmarshaller = context.createUnmarshaller();
          Object obj = unmarshaller.unmarshal(reader);
          System.out.println(obj);
      }
  }
  </code></pre>
  
  <p>When I unmarshal the employee xml using above code, the address list does not get populated. The resulting employee object only has a blank list of adresses. Is there anything wrong with my mappings?</p>
  
  <p>To find out what is going on and see if the employee objects are actually being created using the Object Factory (having the @XMLRegistry annotation), I changed the value of id and name in factory methods, however that had no effect in the output, which tells me JAXB is not actually using the ObjectFactory, why?</p>
  
  <p>Am I going aboout this all wrong? Any help would be appreciated.</p>