XStream and Spring OXM (Object/XML Mapping framework)

It all started when I tried to find a solution for customizing an XStream instance for my ActiveMQ XStream message transformer that would be better than currently used (extending a base transformer and providing a custom factory method that will do things like alias, converter, annotation, etc. settings). I wanted an easy solution to configure XStream instance in a Spring xml configuration file used in my application and provide that instance to all beans that need it (message transformer among others).

XStream is a very nice library that I’ve used and written about before, but there is no Spring factory builder that could be used for this purpose. A little googling got me to the OXM framework, a part of Spring WS project.

The main purpose of this library is to provide a layer above various Object/XML mapping frameworks, such as Castor, Jaxb, XStream, etc. It contains two main abstractions, Marshaller and Unmarshaller wrapping these basic two operations found in appropriate frameworks. For the context objects, this framework use standard classes located in the javax.xml.transform package, so you can work with Stream, Sax and DOM sources and results. Also, it is distributed in two JARs, one that is fully JDK1.4 compatible and one that adds extra features for JDK5 where it is appropriate, which is very nice.

Now back to the XStream and some examples. First of all, in order to use this library you must have the appropriate version of spring-oxm jar file in your classpath. You can achieve this, for example, by adding the following dependency in your pom.xml


Now, we can make our Spring definition of the OXM marshaller

<bean id="plainMarshaller"

After the application initialization we have a plainMarshaller bean ready to use

XStreamMarshaller marshaller =
JobDetails job = createJob();
StringResult result = new StringResult();
marshaller.marshal(job, result);

which should marshall and print my JobDetails class to standard output. So, the following output is expected


Now let’s add some aliases and converters

<bean id="configuredMarshaller"
    <property name="aliases">
            <prop key="job">org.sensatic.jqr.jobs.JobDetails</prop>
    <property name="converters">
            <bean class="org.sensatic.jqr.jobs.ParamsConverter"/>

Yes, it is really as easy as that. If we now use a marshaller set like this, it will produce the following result


As you probably know, XStream can use annotations to set aliases and converters for classes. To demonstrate this we will of course need the additional JAR that provides JDK5 (Tiger) funcionality to the XStream marshaller. For that just add the following dependency to your pom.xml file (it will include the base file as well), or just provide this additional JAR to your classpath.


So, instead of defining the aliases and converters manually, we can just point to the classes that are properly annotated. In this case, we have to use AnnotationXStreamMarshaller class

<bean id="annotatedMarshaller"
     <property name="annotatedClasses">

and the final result should be the same as the previously generated one (of course in case we annotated our class properly).

Now, the real beauty of this is that OXM’s XStreamMarshaller is that it could be used as XStream bean factory (until some better solution comes along). For this to happen, we first have to define a factory bean like this.

<bean id="XStream" factory-bean="annotatedMarshaller" factory-method="getXStream"/>

Then we can use this to set properly configured XStream instance in any other bean (message transformer for example)

<bean id="messageTransformer"
    <property name="XStream"><ref bean="XStream"/></property>

For the end, here’re a couple of things that could be very helpful if happened in the future:

  • XStream should get the proper spring bean factory (which could be used in OXM as well)
  • OXM’s XStreamMarshaller currently can not be set to use specified HiearachicalStreamDriver to be used with streams. For example set the following property on marshaller
    <property name="streamDriver">

    to use JSON instead of XML. The patch is provided and I hope it will be availabe in future versions of OXM.

  • Even with this patch, the XStream instance created by OXM does not have appropriate driver set (and will not be used by other beans where it is injected). Instead the (un)marshaller use it in appropriate methods. This is because of how XStream deals with this property (it could be set only in constructor) so I guess a bit of tweaking on both sides would be required (I guess it could be also sovled through additional constructor in the marshaller, but the universal Spring configuration mechanism would solve this perfectly)

As for the ActiveMQ and transforming messages between XML and Objects:

  • The XStreamMessageTransformer could be modified to work with custom HierarchicalStreamDriver, until we are able to provide fully configured XStream instance completely configured through Spring XML.
  • OXMMessageTransformer would be great stuff to have, as it would provide more flexibility to people who needs it.

I hope I’ll have these two finished real soon.

Finally, take a look at the following two posts from Arjen on the topic


  1. I’m definitely planning to use Camel for some of these tasks. This was only the first logical step forward as I’ve used transformers in the “pre-Camel” age. I’ll try to see if some of these XML mapping stuff could be applied to Camel too and provide some code for it. Cheers.

Leave a comment

Your email address will not be published. Required fields are marked *