XFire 概述

XFire 是 codeHaus 组织提供的一个开源框架,它构建了 POJO 和 SOA 之间的桥梁,主要特性就是支持将 POJO 通过非常简单的方式发布成 Web 服务,这种处理方式不仅充分发挥了 POJO 的作用,简化了 Java 应用转化为 Web 服务的步骤和过程,也直接降低了 SOA 的实现难度,为企业转向 SOA 架构提供了一种简单可行的方式。

XFire 目前最新的版本是 1.2.2,目前支持的特性主要包括:

  • 支持将 Web 服务绑定到 POJO、XMLBeans、JAXB1.1、JAXB2.0 和 Castor;
  • 支持基于 HTTP、JMS、XMPP 等多种协议访问 Web 服务;
  • 支持多种 Web 服务业界重要标准如 SOAP、WSDL、Web 服务寻址(WS-Addressing)、Web 服务安全(WS-Security)等;
  • 支持 JSR181,可以通过 JDK5 配置 Web 服务;
  • 高性能的 SOAP 实现;
  • 服务器端、客户端代码辅助生成;
  • 对 Spring、Pico、Plexus 等项目的支持等。

XFire 安装包

XFire 框架目前的最新版本是 1.2.6,可以访问 xfire.codehaus.org 下载 XFire 框架的安装包,下载时请选择“全部二进制发布包(Binary Distribution in zip package)”,而不仅仅是“XFire jar 文件(Jar of all XFire modules)”。

下载完成后,我们可以将下载的 .zip 文件解压缩到任意的文件夹中(后面的章节中使用 % XFIRE_HOME % 表示 XFire 框架的安装目录),解压缩后形成的文件目录结构如下:

  • api(目录)

    api 目录中是 XFire 框架中所有类(class)对应的 API 文档,为开发者使用 XFire 完成应用开发提供帮助。

  • examples(目录)

    examples 目录中包含了所有随 XFire 二进制包发布的实例,包括这些实例的源代码和相关 Web 应用配置内容。

  • lib(目录)

    lib 目录中包含 XFire 运行所需要的外部支持类包(.jar文件),可以根据不同项目所需的 XFire 特性选择所需要的支持类包。保守的方法是在 Web 项目中包含所有的外部支持类包(.jar文件)。

  • manual(目录)

    manual 目录中包含有 XFire 框架的帮助文档,开发者可以从这些帮助文档中学习更多运用 XFire 框架实现 SOA 的知识和技巧。

  • modules(目录)

    modules 目录中包含了 XFire 框架根据不同特性分别编译的二进制包文件。发布基于 XFire 框架的 Web 项目时,可以选择使用该目录下的所有 .jar 文件,也可以选择 XFire-all-1.2.6.jar 文件。

  • XFire-all-1.2.6.jar

    XFire 框架的二进制包文件,包含了全部的模块(modules)。

  • LICENSE.txt

    LICENSE.txt 文件中包含了 XFire 框架的授权协议。

  • NOTICE.txt
  • README.txt

    这两个文件中包含了 XFire 发布时的一些有用的信息。

  •  

     如果不是用MyEclipse开发,先下载XFire的jar到lib目录。

    开发

    新建webservice工程myeclipse-new -project-web service project

     新建包:test.

    新建接口和服务类。XFire是以接口反射机制开自动取得远程方法的。

    接口:

    public interface IAccount {
     public int account(int x,int y);
    }

    实现类:

    public class AccountImp implements IAccount {

     public int account(int x, int y) {
      // TODO Auto-generated method stub
      return x*y;
     }

    }

    在webservices包下再新建 web service

    service name :mywebservice

    service interface: test.IAccount//手动选择导入test包下的IAccount接口

    service class: test.AccountImp//业务实现类

    //协议为soap协议,MyEclipse下配置保持不变。

    自动生成的services.xml配置如下

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://xfire.codehaus.org/config/1.0">

     <service>
      <name>MyService</name>
      <serviceClass>test.IAccount</serviceClass>
      <implementationClass>test.AccountImp</implementationClass>
      <style>wrapped</style>
      <use>literal</use>
      <scope>application</scope>
     </service></beans>

     自动加入在web.xml配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
      </servlet-mapping>

      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    这两个文件都是IDE自动生成的。也可以手写。

    测试一下wsdl。输入地址http://localhost:8080/WebService/services/MyService?wsdl

    http://域名/工程名/services/service.xml配置的name:MyService;

    结果如下:

      <?xml version="1.0" encoding="UTF-8" ?>
    - < wsdl:definitions targetNamespace =" http://test " xmlns:tns =" http://test " xmlns:wsdlsoap =" http://schemas.xmlsoap.org/wsdl/soap/ " xmlns:soap12 =" http://www.w3.org/2003/05/soap-envelope " xmlns:xsd =" http://www.w3.org/2001/XMLSchema " xmlns:soapenc11 =" http://schemas.xmlsoap.org/soap/encoding/ " xmlns:soapenc12 =" http://www.w3.org/2003/05/soap-encoding " xmlns:soap11 =" http://schemas.xmlsoap.org/soap/envelope/ " xmlns:wsdl =" http://schemas.xmlsoap.org/wsdl/ ">
    - < wsdl:types >
    - < xsd:schema xmlns:xsd =" http://www.w3.org/2001/XMLSchema " attributeFormDefault =" qualified " elementFormDefault =" qualified " targetNamespace =" http://test ">
    - < xsd:element name =" account ">
    - < xsd:complexType >
    - < xsd:sequence >
      < xsd:element maxOccurs =" 1 " minOccurs =" 1 " name =" in0 " type =" xsd:int " />
      < xsd:element maxOccurs =" 1 " minOccurs =" 1 " name =" in1 " type =" xsd:int " />
      </ xsd:sequence >
      </ xsd:complexType >
      </ xsd:element >
    - < xsd:element name =" accountResponse ">
    - < xsd:complexType >
    - < xsd:sequence >
      < xsd:element maxOccurs =" 1 " minOccurs =" 1 " name =" out " type =" xsd:int " />
      </ xsd:sequence >
      </ xsd:complexType >
      </ xsd:element >
      </ xsd:schema >
      </ wsdl:types >
    - < wsdl:message name =" accountResponse ">
      < wsdl:part name =" parameters " element =" tns:accountResponse " />
      </ wsdl:message >
    - < wsdl:message name =" accountRequest ">
      < wsdl:part name =" parameters " element =" tns:account " />
      </ wsdl:message >
    - < wsdl:portType name =" MyServicePortType ">
    - < wsdl:operation name =" account ">
      < wsdl:input name =" accountRequest " message =" tns:accountRequest " />
      < wsdl:output name =" accountResponse " message =" tns:accountResponse " />
      </ wsdl:operation >
      </ wsdl:portType >
    - < wsdl:binding name =" MyServiceHttpBinding " type =" tns:MyServicePortType ">
      < wsdlsoap:binding style =" document " transport =" http://schemas.xmlsoap.org/soap/http " />
    - < wsdl:operation name =" account ">
      < wsdlsoap:operation soapAction ="" />
    - < wsdl:input name =" accountRequest ">
      < wsdlsoap:body use =" literal " />
      </ wsdl:input >
    - < wsdl:output name =" accountResponse ">
      < wsdlsoap:body use =" literal " />
      </ wsdl:output >
      </ wsdl:operation >
      </ wsdl:binding >
    - < wsdl:service name =" MyService ">
    - < wsdl:port name =" MyServiceHttpPort " binding =" tns:MyServiceHttpBinding ">
      < wsdlsoap:address location =" http://localhost:8080/WerService/services/MyService " />
      </ wsdl:port >
      </ wsdl:service >
      </ wsdl:definitions >

    创建webservice成功!无论用net,还flex都可以调用。

    java远程调用。

    新建web工程WebServiceTest

    由于在MyEclipse中只有新建webservice才会自动导入xfire的所需jar包。

    所以使用时自己手动导入,把jar包考入lib文件夹下。

    由于XFire的机制,先要建立跟要调用接口同样的接口名,并包含所需方法,才可以调用相应的方法

    客户调用端接口

    public interface ImpAccount {
     public int account(int x,int y);
    }

    创建调用业务实现类:(也可以是servlet或者jsp)

    //导入包。
    import org.codehaus.xfire.XFireFactory;
    import org.codehaus.xfire.client.XFireProxyFactory;
    import org.codehaus.xfire.service.Service;
    import org.codehaus.xfire.service.binding.ObjectServiceFactory;

    public class ServiceClassTest {

     /**
      * @param args
      */
     public static void main(String[] args) {
      // TODO Auto-generated method stub
          Service srModel = new ObjectServiceFactory()
                .create(IAccount.class);
        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
                .newInstance().getXFire());//创建工厂实例
        
        String helloURL = "http://localhost:8080/WerService/services/MyService";
        try {
         IAccount srvc = (IAccount) factory.create(
                    srModel, helloURL);
            System.out.print("/"+srvc.account(2,3));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
      }
     

    }

    运行java Application.

    返回结果:/6。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐