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();
        }

    }
}

你可能感兴趣的:(factory)