Skip to main content
Jump to: navigation, search


< EclipseLink‎ | Examples‎ | MOXy‎ | Spring
Revision as of 16:16, 27 July 2010 by (Talk | contribs) (Requirements)

In order to use EclipseLink 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 JAXB.


The following are required to use EclipseLink JAXB with Spring.


The latest version of EclipseLink can be found on the EclipseLink download page

Spring Framework

The latest version of the Spring Framework can be found on the Spring download page

Model Classes

The following model classes make use of standard JAXB annotations as well as MOXy extensions:

Configuration: applicationContext.xml

The following XML file will be used to configure our beans:

<?xml version="1.0" encoding="UTF-8"?>
    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="example.gettingstarted"/>
    <bean id="xmlHelper" class="example.gettingstarted.XMLHelper">
        <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 "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
    • We use the "contextPath" property to indicate the location of the model classes,, and an ObjectFactory class or jaxb.index file

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

javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory

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");

Source/Config Files

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

This is the class responsible for marshal/unmarshal operations:

package example.gettingstarted;
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 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 an instance of Jaxb2Marshaller
    public void setMarshaller(Jaxb2Marshaller marshaller) {
        this.marshaller = marshaller;


This is the ANT build file used to run the example.

<?xml version="1.0" standalone="yes"?>
<project basedir="." default="run" name="JAXB Annotation Example">
	<property name="eclipselink.home" value="C:/spring/workspace/eclipselink"/>
	<property name="spring.home" value="C:/spring/spring-framework-3.0.2.RELEASE"/>
	<property name="srcdir" value="src" />
	<path id="classpath">
		<filelist dir="${spring.home}">
			<file name="dist/org.springframework.aop-3.0.2.RELEASE.jar"/>
			<file name="dist/org.springframework.asm-3.0.2.RELEASE.jar"/>
			<file name="dist/org.springframework.aspects-3.0.2.RELEASE.jar"/>
			<file name="dist/org.springframework.beans-3.0.2.RELEASE.jar"/>
			<file name="dist/org.springframework.context-3.0.2.RELEASE.jar"/>
			<file name="dist/"/>
			<file name="dist/org.springframework.core-3.0.2.RELEASE.jar"/>
			<file name="dist/org.springframework.expression-3.0.2.RELEASE.jar"/>
			<file name="dist/org.springframework.oxm-3.0.2.RELEASE.jar"/>
                        <file name="dist/org.springframework.test-3.0.2.RELEASE.jar"/>
			<file name="projects/spring-build/lib/ivy/commons-logging.jar"/>
		<filelist dir="${eclipselink.home}/jlib">
			<file name="eclipselink.jar"/>
	<target name="clean">
		<delete dir="classes" includes="**/*" />
	<target name="compile">
		<mkdir dir="classes" />
		<javac destdir="classes" debug="on">
			<src path="${srcdir}"/>
			<classpath refid="classpath" />
		<copy todir="classes" description="copy resources">
		    <fileset dir="${srcdir}">
			   <exclude name="**/*.java" />
	<target name="run" depends="clean, compile">
		<echo>JAXB Annotation Example</echo>
		<java classname="example.gettingstarted.XMLHelperTest" fork="true">
			<classpath refid="classpath" />
			<classpath path="classes" />

Back to the top