Skip to main content
Jump to: navigation, search


< EclipseLink‎ | Examples‎ | MOXy‎ | Spring

In order to use EclipseLink Dynamic JAXB with the Spring Framework, you simply need a file and an eclipselink.jar on the classpath. No other special configuration is required. This document will demonstrate how to configure Spring to use EclipseLink Dynamic JAXB, bootstrapped from an XML metadata file.

Configuration: applicationContext.xml

In Spring, beans are configured using the applicationContext.xml file. The following XML file will be used to configure our beans:

    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="lazyInit" value="true"/>
        <property name="contextPath" value="example.gettingstarted"/>
    <bean id="xmlHelper" class="example.gettingstarted.XMLHelper">
        <property name="metadataFile"><value>classpath:eclipselink-oxm.xml</value></property>
        <property name="marshaller" ref="jaxbMarshaller"/>

Two beans are being defined here:

  • xmlHelper
    • This is the class that will do all of the work, i.e. marshal and unmarshal
    • We use the "metadataFile" property to indicate that we want Spring to inject the XML metadata file eclipselink-oxm.xml into the setMetadataFile method on our xmlHelper bean
    • We use the "marshaller" property to indicate that we want Spring to inject an instance of org.springframework.oxm.jaxb.Jaxb2Marshaller
  • jaxbMarshaller
    • This is an instance of the org.springframework.oxm.jaxb.Jaxb2Marshaller class that will be injected into our xmlHelper bean
    • We use the "contextPath" property to pass the context path to the underlying JAXBContext
    • We use the "lazyInit" property set to "true" to allow us to pass in the properties object containing the XML schema before the underlying JAXBContext is created

Following is the file that tells Spring to use EclipseLink JAXB:


Bootstrapping the Application

The standard Spring bean lookup method can be used to gain access to the xmlHelper bean:

// initialize IoC Container
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// retrieve the XMLHelper instance from the Container
XMLHelper xmlHelper = (XMLHelper) appContext.getBean("xmlHelper");

Example: Bootstrap from XML metadata

Here is an example of EclipseLink Dynamic JAXB used with the Spring Framework.


  • EclipseLink
    • The latest version of EclipseLink can be found on the EclipseLink download page. eclipselink.jar must be on the classpath.
  • Spring Framework
    • The latest version of the Spring Framework can be found on the Spring download page.
    • The JAR files in the dist folder of your Spring install as well as commons-logging.jar found in /projects/spring-build/lib/ivy must be on the classpath.

Source/Config Files

This section contains the various source and configuration files for the example.


Following is the XML metadata file that is used to create the dynamic entities:

<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="" package-name="example.gettingstarted">
		<java-type name="Customer">
                <xml-element java-attribute="name" type="java.lang.String" xml-path="personal-info/name/text()" />
                <xml-element java-attribute="address" type="example.gettingstarted.Address" xml-path="contact-info/address" />
                <xml-element java-attribute="phoneNumbers" type="example.gettingstarted.PhoneNumber" container-type="java.util.ArrayList" xml-path="contact-info/phone-number" />
        <java-type name="PhoneNumber">
                <xml-attribute java-attribute="type" type="java.lang.String"/>
                <xml-value java-attribute="value" type="java.lang.String"/>
        <java-type name="Address">
                <xml-element java-attribute="street" type="java.lang.String" xml-path="street/text()" />
                <xml-element java-attribute="city" type="java.lang.String" xml-path="city/text()" />

This is the class responsible for marshal/unmarshal operations:

package example.gettingstarted;
import java.util.HashMap;
import java.util.Map;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import org.springframework.oxm.XmlMappingException;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
public class XMLHelper {
    private static String ECLIPSELINK_OXM_KEY = "eclipselink-oxm-xml";
    private Map<String, Object> properties;
    private Jaxb2Marshaller marshaller;
     * Unmarshal a given source
    public Object load(Source source) throws XmlMappingException, IOException {
        return marshaller.unmarshal(source);
     * Marshal a given Object to a Result
    public void save(Object obj, Result result) throws XmlMappingException, IOException {
        marshaller.marshal(obj, result);
     * This method is used by Spring to inject the eclipselink-oxm.xml metadata 
     * file to be applied to the model.
    public void setMetadataFile(Resource metadataFile) throws IOException {
        properties = new HashMap<String, Object>();
        properties.put(ECLIPSELINK_OXM_KEY, new StreamSource(metadataFile.getInputStream()));
     * This method is used by Spring to inject an instance of Jaxb2Marshaller
    public void setMarshaller(Jaxb2Marshaller marshaller) {
        this.marshaller = marshaller;

This code demonstrates how the XMLHelper bean can be acquired and used to perform marshal/unmarshal operations on a Customer.

// initialize IoC Container
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// retrieve the XMLHelper instance from the Container
xmlHelper = (XMLHelper) appContext.getBean("xmlHelper");
// load customer
DynamicEntity customer = (DynamicEntity) xmlHelper.load(new StreamSource(new FileInputStream("customer.xml")));
// update customer
DynamicEntity address = customer.get("address");
address.set("street", "234 Some Other Rd.");
customer.set("address", address);
// save customer, new StreamResult(new FileOutputStream("customer.xml")));


This is a sample instance document.

<?xml version="1.0" encoding="UTF-8"?>
    <personal-info><name>Jane Doe</name></personal-info>
          <city>My Town</city>
          <street>123 Any Street</street>
        <phone-number type="work">613-555-1111</phone-number>
        <phone-number type="cell">613-555-2222</phone-number>

Back to the top