通過spring remoting框架整合xfire

原文地址:http://docs.codehaus.org/display/XFIRE/Spring+Remoting

 

This page outlines how to set up XFire for use via Spring's Remoting framework.

Setup the DispatcherServlet in the web.xml:

You'll need to include the DispatcherServlet in your web.xml. Notice that it provides the locations of where your spring beans are.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml
    classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
</context-param>

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>xfire</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>xfire</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Notice how we're including /org/codehaus/xfire/spring/xfire.xml off the classpath. This contains some standard bean definitions for the TransportManager, ServiceRegistry and some simple ServiceFactorys.

Remote Exporter

When using a RemoteExporter, you should define a service interface and (at least) one implementation of that interface.

Service Interface

package org.codehaus.xfire.spring.example;

/**
 * Provides the service contract for the echo service.
 *
 * @author <a href="mailto:[email protected]">Arjen Poutsma</a>
 */
public interface Echo
{
    String echo(String in);
}

Service Implementation

package org.codehaus.xfire.spring.example;

/**
 * Provides a default implementation of the echo service interface.
 */
public class EchoImpl
        implements Echo
{
    public String echo(String in)
    {
        return in;
    }

}

XFireExporter

Then, you'll need to create a exporter definition for the service you want to expose. Typically, you'll do this in the servlet context configuration file. Since the dispatcher servlet is named xfire, this file should be called xfire-servlet.xml.

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
        <map>
            <entry key="/EchoService">
                <ref bean="echo"/>
            </entry>
        </map>
    </property>
</bean>

<!-- Declare a parent bean with all properties common to both services -->
<bean id="echo" class="org.codehaus.xfire.spring.remoting.XFireExporter">
    <property name="serviceFactory">
        <ref bean="xfire.serviceFactory"/>
    </property>
    <property name="xfire">
        <ref bean="xfire"/>
    </property>
    <property name="serviceBean">
        <ref bean="echoBean"/>
    </property>
    <property name="serviceClass">
        <value>org.codehaus.xfire.spring.example.Echo</value>
    </property>
</bean>

Thus, the methods defined in the Echo interface are exposed at the path /EchoService, backed by the EchoImpl bean.

 The EchoImpl bean should be defined as follows, in applicationContext.xml:

<bean id="echoBean" class="org.codehaus.xfire.spring.example.EchoImpl"/>

If you are using JSR 181 Annotations in the service interface and implementation(s), the interface class should not be specified as the serviceClass property. In this case, either the implementation class should be specified in the serviceClass property or the serviceBean property should be specified, but both together are not necessary. This is because the implementing class already specifies the endpointInterface in the @WebService annotation.

Remoting Clients

With protocols like Hessian or RMI, you just need a service interface to configure a Spring remoting client. However, SOAP is a bit more complicated.

XFire has the concept of a service model. It uses your service class and various extra information to build this service model. Any information you use to configure your client must be used to configure your client as well. For instance, say you specify a custom a custom namespace on your service, you must also specify that on your client.

XFire comes with a helper class to facilitate creating client using Spring: org.codehaus.xfire.spring.remoting.XFireClientFactoryBean. It is modelled after the JaxRpcPortProxyFactoryBean present in Spring, and allows you to configure a client bean in a Spring applicationContext.xml. All that needs to be specified is the service interface (or class in case of JSR 181 Annotations) and a WSDL url location for the service. Additional configuration, such as username/password or namespace, can be explicitly configured using properties.
Example client bean for applicationContext.xml:

<bean id="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
    <property name="serviceClass">
      <value>org.codehaus.xfire.spring.example.Echo</value>
    </property>
    <property name="wsdlDocumentUrl">
      <value>http://localhost:8080/xfire/EchoService?WSDL</value>
    </property>
</bean>

 * Note that this may cause problems if used in the same applicationContext as the service itself (which one might try to do for running unit tests): this bean would attempt to access the WSDL before it became available, preventing the application from loading. Rather, create a separate client applicationContext, for which to use in test cases.

Check out the Client API page to learn more about options when creating clients using XFire.

The full example is available in the ./examples/spring/ folder (within the XFire distribution).

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章