Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/Spring/JAXBAnnotations"

< EclipseLink‎ | Examples‎ | MOXy‎ | Spring
Line 33: Line 33:
 
<source lang="text">
 
<source lang="text">
 
javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory
 
javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory
 +
</source>
 +
 +
== Bootstrapping the Example ==
 +
The standard Spring bean lookup can be used to gain access to the <code>xmlHelper</code> bean:
 +
<source lang="java">
 +
// initialize IoC Container
 +
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 +
// retrieve the XMLHelper instance from Container
 +
XMLHelper xmlHelper = (XMLHelper) appContext.getBean("xmlHelper");
 
</source>
 
</source>
  

Revision as of 14:32, 27 July 2010

In order to use EclipseLink JAXB with the Spring Framework, all that is required is a jaxb.properties file and eclipselink.jar (both files need to be on the classpath). No other special configuration is required. This example will demonstrate how to configure Spring to use EclipseLink JAXB.

Note that the latest version of EclipseLink can be found on the EclipseLink download page.

Configuration: applicationContext.xml

The following configuration file will be used to configure the beans used in this example:

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="example.gettingstarted"/>
    </bean>
    <bean id="xmlHelper" class="example.gettingstarted.XMLHelper">
        <property name="marshaller" ref="jaxbMarshaller"/>
    </bean>
</beans>

Two beans are being defined here:

  • xmlHelper
    • This is the class that will do all of the work, i.e. marshal and unmarshal
  • jaxbMarshaller
    • This is an instance of the org.springframework.oxm.jaxb.Jaxb2Marshaller class that will be injected into our xmlHelper
    • We use the "contexPath" property to indicate the location of the model classes, jaxb.properties, and an ObjectFactory or jaxb.index file

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

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

Bootstrapping the Example

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

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

XMLHelper

package example.gettingstarted;
 
import java.io.IOException;
 
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;
    }
}