Previous: Bindings | Up: Bindings | Next: Aegis Binding |
Bindings Overview
Bindings are one of the most important pieces of XFire. They control how the incoming and outoing XML is mapped to objects.
The default binding in XFire is "Aegis". Aegis means "shield", and the point of it is to create a simple layer which makes going between objects and XML easy. Aegis also provides the layer which allows other bindings to integrate with XFire - like JAXB, XMLBeans and Castor.
Lets take a look at how you would use a different binding with XFire. The BindingProvider class is the one for actually taking the xml streams and reading/writing to them. When we create a service we need to actually supply a BindingProvider to the service. In the simplest case of creating a service you're probably writing code like this:
ServiceFactory factory = new ObjectServiceFactory();
But, what this is actually doing in the constructor is creating an AegisBindingProvider:
ServiceFactory factory = new ObjectServiceFactory(new AegisBindingProvider());
Using another binding
This AegisBindingProvider has the concept of a TypeRegistry and TypeCreators (don't worry you don't need to know much about this), which create Type classes for you. Types serialize and deserialize java classes. When you use a different binding it actually provides a different TypeRegistry and TypeCreator for its types.
So if you were to create a JAXB 2.0 binding you would do:
import org.codehaus.xfire.aegis.AegisBindingProvider; import org.codehaus.xfire.jaxb2.JaxbTypeRegistry; import org.codehaus.xfire.service.binding.ObjectServiceFactory; ... ServiceFactory factory = new ObjectServiceFactory(new AegisBindingProvider(new JaxbTypeRegistry()));
Each binding has a different type registry:
Binding | Type Registry |
Default POJOs | org.codehaus.xfire.aegis.DefaultTypeMappingRegistry |
Castor | org.codehaus.xfire.castor.CastorTypeMappingRegistry |
JAXB 1.1 | org.codehaus.xfire.jaxb.JaxbTypeRegistry |
JAXB 2.0 | org.codehaus.xfire.jaxb2.JaxbTypeRegistry |
XMLBeans | org.codehaus.xfire.xmlbeans.XmlBeansTypeRegistry |
But don't stop here, you can use these BindingProviders with different ServiceFactorys. Here is an example using JAXB 2.0 and the annotation service factory.
import org.codehaus.xfire.aegis.AegisBindingProvider; import org.codehaus.xfire.jaxb2.JaxbTypeRegistry; import org.codehaus.xfire.service.binding.ObjectServiceFactory; ... ServiceFactory factory = new AnnotationServiceFactory(new Jsr181WebAnnotations(), new AegisBindingProvider(new JaxbTypeRegistry()));
Convenience ServiceFactorys
If you just want to use the simple ObjectServiceFactory we have several convenience ServiceFactorys around:
Binding | Service Factory |
JAXB 1.1 | org.codehaus.xfire.jaxb.JaxbServiceFactory |
JAXB 2.0 | org.codehaus.xfire.jaxb2.JaxbServiceFactory |
XMLBeans | org.codehaus.xfire.xmlbeans.XmlBeansServiceFactory |
You can use these just like you would an ObjectServiceFactory:
import org.codehaus.xfire.jaxb2.JaxbServiceFactory; import org.codehaus.xfire.service.ServiceFactory; ... ServiceFactory factory = new JaxbServiceFactory();
The MessageBinding
There is also one other type of binding, the MessageBinding. The MessageBinding has special semantics to allow you to work with XML streams and fragments real easily. Read more on the Message Binding page.
Previous: Bindings | Up: Bindings | Next: Aegis Binding |