ws之XFireProxyFactory简单配置

创建一个web service工程
web.xml文件会有以下内容
  <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> 运行http://.../services/*就会执行相应的service
  </servlet-mapping>
</web-app>

定义一个接口
package com.demo;
public interface IHelloWorldService {
 public String sayHello(String name);
}

接口实现类
package com.demo;
public class HelloWorldServiceImpl implements IHelloWorldService {
  public String sayHello(String name) {
  return "hello,"+name;
 }
}

编写services.xml文件
<beans xmlns="http://xfire.codehaus.org/config/1.0">
 <service>
  <name>HelloWordService</name>
  <serviceClass>com.demo.IHelloWorldService</serviceClass>
  <implementationClass>
   com.demo.HelloWorldServiceImpl
  </implementationClass>
  <style>wrapped</style>
  <use>literal</use>
  <scope>application</scope>
 </service>
</beans>

发布该web应用

编写web service客户端

package com.demo;
import java.net.MalformedURLException;
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 HelloWorldClient {
 public static void main(String args[]) {
        Service srvcModel = new ObjectServiceFactory()
                .create(IHelloWorldService.class);
        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
                .newInstance().getXFire());
         
        String helloWorldURL = "http://localhost:8080/HelloWorld/services/HelloWordService";
        try {
            IHelloWorldService srvc = (IHelloWorldService) factory.create(
                    srvcModel, helloWorldURL);
            System.out.print(srvc.sayHello("dennis"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }
}

posted @ 2013-05-24 10:32  若 ♂ 只如初见  阅读(2521)  评论(0编辑  收藏  举报