Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

< EclipseLink‎ | Examples‎ | MOXy‎ | Spring
Line 1: Line 1:
 
<div style="border: 1px solid rgb(0, 0, 0); margin: 5px; padding: 5px; float: right;">__TOC__</div>  
 
<div style="border: 1px solid rgb(0, 0, 0); margin: 5px; padding: 5px; float: right;">__TOC__</div>  
[[Category:EclipseLink/Examples/MOXy|Spring/JAXBAnnotations]]
+
[[Category:EclipseLink/Examples/MOXy/Spring/JAXBAnnotations]]
  
In order to use EclipseLink JAXB with Spring, all that is required is a <code>jaxb.properties</code> file and <code>eclipselink.jar</code>.  No other special configuration is required.  This example will demonstrate how to configure Spring to use EclipseLink JAXB.
+
In order to use EclipseLink JAXB with the Spring Framework, all that is required is a <code>jaxb.properties</code> file and <code>eclipselink.jar</code> (both of these 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.
  
The latest version of EclipseLink can be found on the [http://www.eclipse.org/eclipselink/downloads/ EclipseLink download] page. <code>eclipselink.jar</code> needs to be on the classpath.  Following is the <code>jaxb.properties</code> file that tells Spring to use EclipseLink JAXB - this file needs to be on the classpath:
+
Note that the latest version of EclipseLink can be found on the [http://www.eclipse.org/eclipselink/downloads/ EclipseLink download] page.
<source lang="text">
+
javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory
+
</source>
+
+
== Jaxb2Marshaller ==
+
Spring supports both the JAXB 1.0 and the JAXB 2.0 API as XML marshalling strategies.  EclipseLink JAXB implements JAXB 2, so for this example we will make use of the <code>org.springframework.oxm.jaxb.Jaxb2Marshaller</code> class. 
+
  
 
== Configuration: applicationContext.xml ==
 
== Configuration: applicationContext.xml ==
This configuration file will be used to configure the beans used in this example
+
The following configuration file will be used to configure the beans used in this example:
 
+
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
Line 29: Line 22:
 
</beans>
 
</beans>
 
</source>
 
</source>
 +
 +
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 <code>org.springframework.oxm.jaxb.Jaxb2Marshaller</code> class that will be injected into our xmlHelper
 +
** We use the "contexPath" property to indicate the location of the model classes, <code>jaxb.properties</code>, and <code>ObjectFactory</code>/<code>jaxb.index</code>
 +
 +
Following is the <code>jaxb.properties</code> file that tells Spring to use EclipseLink JAXB:
 +
<source lang="text">
 +
javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory
 +
</source>
 +
 +
Spring supports both the JAXB 1.0 and the JAXB 2.0 API as XML marshalling strategies.  EclipseLink JAXB implements JAXB 2, so for this example we will make use of the <code>org.springframework.oxm.jaxb.Jaxb2Marshaller</code> class. 
 +
 +
 +
  
 
== XMLHelper ==
 
== XMLHelper ==

Revision as of 14:07, 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 of these 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 ObjectFactory/jaxb.index

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

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

Spring supports both the JAXB 1.0 and the JAXB 2.0 API as XML marshalling strategies. EclipseLink JAXB implements JAXB 2, so for this example we will make use of the org.springframework.oxm.jaxb.Jaxb2Marshaller class.



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;
 
    public Object load(Source source) throws XmlMappingException, IOException {
        return marshaller.unmarshal(source);
    }
 
    public void save(Object obj, Result result) throws XmlMappingException, IOException {
        marshaller.marshal(obj, result);
    }
 
    public void setMarshaller(Jaxb2Marshaller marshaller) {
        this.marshaller = marshaller;
    }
}

Back to the top