---
title: "四步搞定XFire"
author: "zyongking"
date: "2009-09-11T18:43:08Z"
tags: []
slug: "xfire"
draft: false
lastmod: "2009-09-11T18:44:28Z"
id: 467119
url: "https://wind-sing.iteye.com/blog/467119"
---

四步搞定XFire

![](http://dl.iteye.com/upload/attachment/145419/6ecde094-a53b-3060-8f0b-f7f397168850.png)  
 

 

 

 

 

 

 

官方站点：[http://xfire.codehaus.org](http://xfire.codehaus.org/)

XFire是新一代的Java Web服务引擎，XFire使得在JavaEE应用中发布Web服务变得轻而易举。和其他Web服务引擎相比，XFire的配置非常简单，可以非常容易地和Spring集成，它使得Java开发人员终于可以获得和.Net开发人员一样的开发效率。

 

由于使用了StAX（the Streaming API for XML，基于流的XML解析）作为XML解析器，XFire的运行速度又有了质的提高，并且XFire支持最新的JSR 181的Web服务注解。如果使用Java 5，只需要在源代码中编写相应的JSR 181注解，XFire就可以根据Java 5注解自动提取所需的全部信息。由于JSR 181也是JavaEE Web服务标准的一部分，使用它最大的好处在于不仅极大地简化了配置，而且避免了配置文件和某个特定的Web服务引擎的锁定。

本文在myeclpise5.5+win2003下测试

1/在myeclpise5.5中新建webservice工 程,在下图位置注意选1、2项目，第2个不勾的话，到时common－http相关的包就没有导入工程。

 

![](http://dl.iteye.com/upload/attachment/145421/0487424f-6d77-3104-82b8-a7b282917159.png)  
 

2、编写一个普通的接口和类.

```
public interface IHello {
public String sayHello(String username);
}

public class Hello implements IHello {

public String sayHello(String username) {
   // TODO Auto-generated method stub
   return "Hello!"+username;
}

}
```

 

 

3、配置services.xml

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
   <name>myhello</name><!-- 这个名字比较重要,调用时用的就是它 -->
   <namespace>http://localhost/Hello</namespace>
   <serviceClass>test.IHello</serviceClass>
   <implementationClass>test.Hello</implementationClass>

</service>
</beans>
```

 

至此已经可以在浏览器里测试

[http://localhost:8086/webservices/services/myhello?wsdl](http://localhost:8086/webservices/services/myhello?wsdl)

4、测试

```
package test;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFire;
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 Tests {

/**
* @param args
*/
public static void main(String[] args) {
   Service serviceModel = new ObjectServiceFactory().create(IHello.class);
   XFire xfire = XFireFactory.newInstance().getXFire();
   XFireProxyFactory factory = new XFireProxyFactory(xfire);
   String serviceUrl = "http://localhost:8086/webservices/services/myhello";
   try {
    IHello client = (IHello) factory.create(serviceModel, serviceUrl);
    System.out.println(client.sayHello("寻觅"));
   } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

}

}
```

 

 

感觉比起利用jbuilder2006+weblogic+bean(or ejb之sessionbean)要轻量的多。