通过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:poutsma@mac.com">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).

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值