前言:
XFire是新一代WebService框架,同时也支持与Spring集成,帮助我们方便快速地在Spring框架中开发WebService应用。

本节主要介绍XFire+Spring集成的2种常用的方法来实现简单的WebService应用
1、使用XFire的XFireSpringServlet和ServiceBean
2、使用Spring的DispatcherServlet与XFire的XFireExporter

准备工作:
XFire官方网站下载地址:http://xfire.codehaus.org/Download

开发环境:
Window7 + Eclipse3.3 + Tomcat6 + JDK-1.6

XFire服务端和客户端工程预览图:

First-使用XFire的XFireSpringServlet和ServiceBean

一、Server-服务端实现步骤: 1.创建service接口->2.创建Service接口的实现类->3.web.xml配置(XFireSpringServlet)->4.配置ServiceBean->5.服务发布

1、创建service接口

package webjar.foo;
import common.MyPojo; public interface MyDispatcherServletXFire
{
String divide(int dividend, int divisor); MyPojo getMyPojo(MyPojo pojo);
}

2、创建Service接口的实现类

package webjar.foo;
import common.MyPojo; // org.springframework.web.servlet.DispatcherServlet与org.codehaus.xfire.spring.remoting.XFireExporter结合实现XFire
public class MyDispatcherServletXFireImpl implements MyDispatcherServletXFire
{
@Override
public String divide(int dividend, int divisor)
{
return dividend + " ÷ " + divisor + " = " + (dividend / divisor);
} @Override
public MyPojo getMyPojo(MyPojo pojo)
{
System.out.println("Hi, The client's pojo is :" + System.getProperty("line.separator") + pojo); pojo.setName("DispatcherServlet and XFireExporter to publish xfire services");
pojo.setArray(new String[] { "Hi, Welcome to MyDispatcherServletXFire !" });
return pojo;
}
}

3、在web.xml文件中进行XFire拦截配置(XFireSpringServlet)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Spring加载的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 可以再这里加入xfire.xml也可以在applicationContext.xml中引入 -->
<param-value>
classpath:org/codehaus/xfire/spring/xfire.xml
classpath:context-webservice.xml
</param-value>
</context-param> <!-- Xfire Servlet -->
<servlet>
<servlet-name>XFireServlet</servlet-name>
<display-name>XFire Servlet</display-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

4、新增context-webservice.xml文件,里面进行WebService服务的发布的基本配置(ServiceBean)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <!-- Spring和XFire实现方法一 -->
<!-- org.codehaus.xfire.spring.XFireSpringServlet与org.codehaus.xfire.spring.ServiceBean结合实现XFire --> <!-- Service实现类-->
<bean id="myXFireSpringServlet" class="xfirejar.foo.MyXFireSpringServletImpl" /> <!-- 这里的name属性并不是调用时的Service名字;调用时要用类名,而不能直接使用myXFireService -->
<bean name="myXFireService"
class="org.codehaus.xfire.spring.ServiceBean">
<!-- Service实现类 -->
<property name="serviceBean" ref="myXFireSpringServlet" />
<!-- Service接口 -->
<property name="serviceClass" value="xfirejar.foo.MyXFireSpringServlet" />
<property name="inHandlers">
<list>
<ref bean="addressingHandler" />
</list>
</property>
</bean> <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler" />
</beans>

5、验证服务发布状态

启动tomcat正常,因为本人的web工程名称为XFireFoo,因此在Browser地址输入http://ip:port/XFireFoo/remoting/MyXFireSpringServlet?wsdl

如果显示效果和下面截图信息一致,即说明webservice服务端搭建成功!

二、Client-客户端实现步骤:

A、客户端与WebService服务端在同一应用

1、测试桩示例代码

package xfirejar.foo;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import common.MyPojo; public class XFireJarFoo
{
public static void main(String[] args) throws MalformedURLException
{
// 调用时要用类名(接口名称),而不能直接使用myXFireService
String serviceURL = "http://localhost:8080/XFireFoo/remoting/MyXFireSpringServlet";
Service serviceModel = new ObjectServiceFactory().create(MyXFireSpringServlet.class, null);
XFireProxyFactory serviceFactory = new XFireProxyFactory(); MyXFireSpringServlet myFire = (MyXFireSpringServlet) serviceFactory.create(serviceModel, serviceURL);
System.out.println(myFire.divide(9, 3)); MyPojo pojo = new MyPojo();
pojo.setArray(new String[] { "remoting" });
pojo.setName("XFireJarFoo"); System.out.println(myFire.getMyPojo(pojo));
}
}

PS:也可以使用XFire中的XFireClientFactoryBean来实现调用,可参见[转]:http://blog.csdn.net/wlbing0625/article/details/7744699

2、测试结果

9 ÷ 3 = 3
MyPojo{name = XFireSpringServlet and ServiceBean to publish xfire services array = [Hi, Welcome to MyXFireSpringServlet !]}

B、客户端与WebService服务端在不同应用
re: 通过访问http://ip:port/XFireFoo/remoting/MyXFireSpringServlet?wsdl地址我们可以获取到wsdl文件,
因此直接“使用eclipse自带WEB service client指定wsdl文件,从而反向生成java代码方式”进行webservice服务调用。
具体实现步骤可参见本人已发布的AXIS最佳实践章节中“客户端的开发”,So easy !

Second-使用Spring的DispatcherServlet与XFire的XFireExporter

一、Server-服务端实现步骤1.创建service接口->2.创建Service接口的实现类->3.web.xml配置(DispatcherServlet)->4.配置XFireExporter->5.服务发布

1、创建service接口

package webjar.foo;

import common.MyPojo;

public interface MyDispatcherServletXFire
{
String divide(int dividend, int divisor); MyPojo getMyPojo(MyPojo pojo);
}

2、创建Service接口的实现类

package webjar.foo;

import common.MyPojo;

// org.springframework.web.servlet.DispatcherServlet与org.codehaus.xfire.spring.remoting.XFireExporter结合实现XFire
public class MyDispatcherServletXFireImpl implements MyDispatcherServletXFire
{
@Override
public String divide(int dividend, int divisor)
{
return dividend + " ÷ " + divisor + " = " + (dividend / divisor);
} @Override
public MyPojo getMyPojo(MyPojo pojo)
{
System.out.println("Hi, The client's pojo is :" + System.getProperty("line.separator") + pojo); pojo.setName("DispatcherServlet and XFireExporter to publish xfire services");
pojo.setArray(new String[] { "Hi, Welcome to MyDispatcherServletXFire !" });
return pojo;
}
}

3、在web.xml文件中进行DispatcherServlet拦截配置(DispatcherServlet)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Spring加载的配置文件,主要通过ContextLoader中的CONFIG_LOCATION_PARAM = "contextConfigLocation" -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 可以再这里加入xfire.xml也可以在applicationContext.xml中引入 -->
<param-value>
classpath:org/codehaus/xfire/spring/xfire.xml
classpath:context-webservice.xml
</param-value>
</context-param> <!-- Spring framework -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 注意因为servlet-name为myxfire,固xfire配置文件名应该是myxfire-servlet.xml -->
<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>/services/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

4、新增xfire-servlet.xml文件,里面进行WebService服务的发布的基本配置(XFireExporter)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <!-- basic configuration -->
<!-- 若是web.xml已经配置了org/codehaus/xfire/spring/xfire.xml,这里就不需要配置了 -->
<!-- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> --> <!-- Service实现类-->
<bean id="myImpl" class="webjar.foo.MyDispatcherServletXFireImpl" /> <!-- XFire发布服务核心处理类的配置 -->
<bean name="/IWebJarService" class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- Service实现类 -->
<property name="serviceBean" ref="myImpl" />
<!-- Service接口 -->
<property name="serviceClass" value="webjar.foo.MyDispatcherServletXFire" />
</bean>
</beans>

5、验证服务发布状态

启动tomcat正常,因为本人的web工程名称为XFireFoo,因此在Browser地址输入http://ip:port/XFireFoo/services/IWebJarService?wsdl

如果显示效果和下面截图信息一致,即说明webservice服务端搭建成功!

二、Client-客户端实现步骤:

A、客户端与WebService服务端在同一应用

1、本地客户端测试桩

package webjar.foo;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import common.MyPojo; public class WebJarFoo
{
public static void main(String[] args) throws MalformedURLException
{
// 客户端访问时使用xfire-servlet.xml中的XFireExporter配置的name属性(IWebJarService)进行调用
String serviceURL = "http://localhost:8080/XFireFoo/services/IWebJarService";
Service serviceModel = new ObjectServiceFactory().create(MyDispatcherServletXFire.class, null);
XFireProxyFactory serviceFactory = new XFireProxyFactory(); MyDispatcherServletXFire myFire = (MyDispatcherServletXFire) serviceFactory.create(serviceModel, serviceURL);
System.out.println(myFire.divide(9, 3)); MyPojo pojo = new MyPojo();
pojo.setArray(new String[] { "services" });
pojo.setName("WebJarFoo"); System.out.println(myFire.getMyPojo(pojo));
}
}

2、测试结果

9 ÷ 3 = 3
MyPojo{name = DispatcherServlet and XFireExporter to publish xfire services array = [Hi, Welcome to MyDispatcherServletXFire !]}

B、客户端与WebService服务端在不同应用
re: 通过访问http://ip:port/XFireFoo/services/IWebJarService?wsdl地址我们可以获取到wsdl文件,
因此直接“使用eclipse自带WEB service client指定wsdl文件,从而反向生成java代码方式”进行webservice服务调用。
具体实现步骤可参见本人已发布的AXIS最佳实践章节中“客户端的开发”,So easy !

代码下载:

XFireFoo服务端和客户端示例代码:XFireFoo

XFire最佳实践的更多相关文章

  1. paip.myeclipse7 java webservice 最佳实践o228

    paip.myeclipse7  java webservice 最佳实践o228 java的ws实现方案:jax-ws>>xfire ws的测试工具  webservice测试调用工具W ...

  2. ASP.NET跨平台最佳实践

    前言 八年的坚持敌不过领导的固执,最终还是不得不阔别已经成为我第二语言的C#,转战Java阵营.有过短暂的失落和迷茫,但技术转型真的没有想象中那么难.回头审视,其实单从语言本身来看,C#确实比Java ...

  3. 《AngularJS深度剖析与最佳实践》简介

    由于年末将至,前阵子一直忙于工作的事务,不得已暂停了微信订阅号的更新,我将会在后续的时间里尽快的继续为大家推送更多的博文.毕竟一个人的力量微薄,精力有限,希望大家能理解,仍然能一如既往的关注和支持sh ...

  4. ASP.NET MVC防范CSRF最佳实践

    XSS与CSRF 哈哈,有点标题党,但我保证这篇文章跟别的不太一样. 我认为,网站安全的基础有三块: 防范中间人攻击 防范XSS 防范CSRF 注意,我讲的是基础,如果更高级点的话可以考虑防范机器人刷 ...

  5. 快速web开发中的前后端框架选型最佳实践

    这个最佳实践是我目前人在做的一个站点,主要功能: oauth登录 发布文章(我称为"片段"),片段可以自定义一些和内容有关的指标,如“文中人物:12”.支持自定义排版.插图.建立相 ...

  6. Spring Batch在大型企业中的最佳实践

    在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...

  7. Atitit.log日志技术的最佳实践attilax总结

    Atitit.log日志技术的最佳实践attilax总结 1. 日志的意义与作用1 1.1. 日志系统是一种不可或缺的单元测试,跟踪调试工具1 2. 俩种实现[1]日志系统作为一种服务进程存在 [2] ...

  8. PHP核心技术与最佳实践——全局浏览

    难得买到并喜欢一本好书,‘PHP核心技术与最佳实践’. 几天时间,先看了个大概,总结一下整体是什么样子的,怎么看怎么学. 1.总共14章: 2.第1.2章讲PHP的OOP: 其中第一章侧重于PHP的O ...

  9. Abp集成Swagger的最佳实践

    1.在项目中添加nuget包 Abp.Web.Api.SwaggerTool 2.在项目Abp模块的DependsOn添加AbpWebApiSwaggerToolModule Run It,启动项目, ...

随机推荐

  1. Vue.js常用指令总结

    有时候指令太多会造成记错.记混的问题,所以本文在记忆的时候会采用穿插记忆的方式,交叉比对,不易出错. 本文主要讲了一下六个指令: v-if//v-show//v-else//v-for//v-bind ...

  2. spring 3.0 应用springmvc 构造RESTful URL 详细讲解

    在线springmvc_rest demo 由于下一版本的rapid-framwork需要集成spring RESTful URL,所以研究了一下怎么搭建. 并碰到了一下问题. springmvc 3 ...

  3. ubuntu下安装配置OpenCV

    Cmake的安装 我用的是ubuntu-software自动下载安装的. Ubuntu 下安装 OpenCV 首先下载安装相关包,然后下载OpenCV 系统:ubuntu16.04 OpenCV:2. ...

  4. Codeforces

    Codeforces 7E #include <iostream> #include <cstring> #include <cstdio> #include &l ...

  5. [Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法

    主要讲解Android Studio中生成aar文件以及本地方式使用aar文件的方法. 在Android Studio中对一个自己库进行生成操作时将会同时生成*.jar与*.aar文件. 分别存储位置 ...

  6. ae学习

    Ae           提供者CoSA 1993年1月 版本1.0 代号Egg 主要加入法人功能layered compositing with mask, effect, transforms, ...

  7. Mac快捷键与命令学习

    最近开始使用mac air,以前从来没有接触过IOS系统,各种操作捉急.Mac快捷键相当多,遇到各种操作不会就只好百度,然后整理了一堆有用或者没用的命令,一股脑儿列在下面.其中有不少命令是和linux ...

  8. lua userdata

    #define metatablename "studentlib.06-11-11" /** * utility functions */ static int pusherro ...

  9. JUnit3的作用

    简要说JUnit的4大功能 1. 管理测试用例.修改了哪些代码,这些代码的修改会对哪些部分有影响,通过JUnit将这次的修改做个完整测试.这也就JUnit中所谓的TestSuite. 2. 定义测试代 ...

  10. C# Lock 解读 (关键是理解最后一句)

    最近在研究.NET分布式缓存代码,正好涉及Lock,看了网上的文章,总结了一些Lock相关的知识,供大家一起学习参考. 一.Lock定义     lock 关键字可以用来确保代码块完成运行,而不会被其 ...