Codehaus XFire
DocumentationQuicklinksDevelopers
Sponsors |
There are a number of simple steps involved in setting up the Spring/XFire integration. The end result of this guide is that you will be able to trivially register any beans you want exposed as services within your Spring configuration file. Note that for the sake of simplicity, the Spring examples shown here use the improved XML format of Spring 2.0. It should be trivial to figure out more verbose Spring 1.x syntax to match, if required. There are roughly 3 areas of configuration. First we need to tell Spring all about XFire and its objects that should be managed, then we need to specify the servlet that will handle and map all incoming requests, and finally we register our service beans. Spring ConfigurationXFire comes with a spring xml file that can be imported to set up the main objects required and wire them up. Once we're imported this file, we can override any of the defaults to customise the configuration. Finally, we specify how urls are handled by XFire. DefaultsThe first step is to import this bundled resource in your applicationContext.xml file (or equivelant): <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/> This file will set up a default instance of XFire, along with the other objects it interacts with, such as TransportManager, ObjectServiceFactory, and so on. All objects defined in this file have ID's that are prefixed with 'xfire.', so when we reference them below, you'll know where to look to find the default bean definition. Customising XFireHaving imported the defaults, we'd now like to configure certain non-default features. Annotations (JSR-181)For this example, we will register JSR 181 Annotations support. This enables us to simply annotate our bean classes as web services. As that document shows, we can simply add a few lines in our Spring configuration file to enable annotation support: <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/> <bean id="handlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping"> <property name="typeMappingRegistry" ref="xfire.typeMappingRegistry" /> <property name="xfire" ref="xfire" /> <property name="webAnnotations" ref="webAnnotations" /> </bean> URL HandlersWe'd also like to specify URL handling. This objects allows us to specify how incoming URLs map to services. It allows us to either point to specific bean instances or prototypes (where a new service object is created for every request). For our simple example, we just specify the default: <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/"> <ref bean="handlerMapping"/> </entry> </map> </property> </bean> When this handler mapping is used, all annotated beans will be automatically exported at a certain url prefix (by default: /services/). Thus, the annotatedEcho bean will available at /services/EchoService. Make sure your annotated beans are declared after the Jsr181HandlerMapping declaration. Web ConfigurationHaving configured the Spring side of things, the next step is to configure the web application with the appropriate servlet and URL mappings. Since our example uses a simple url mapping, we only need to define one mapping. Obviously if we had a variety of url mappings, we would need to specify a matching servlet mapping. <servlet> <servlet-name>SOAPServlet</servlet-name> <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SOAPServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> Service configurationNow that XFire has been configured fully, the last remaining part is registering the services themselves. Here is where all the setup earlier pays off, as services can be registered as simply as: <bean class="com.acme.foo.EchoBean"/>
If you need more advanced configuration and finer control over the exposed service, you can use the bundled ServiceBean. More documentation can be found at Spring, XBean, Servlets and more under the ServiceBean section. |