1. 程式人生 > >spring-xfire簡單例子

spring-xfire簡單例子

最近重新熟悉下webservice知識,以前用的是axis2,現在用另一種方式spring xfire來實現簡單的webservice;
xfire和axis
xfire比axis效能高
axis比xfire響應時間短
一、在eclipse下新建專案工程xfire
二、匯入基本的jar包
        commons-codec-1.3.jar
        commons-httpclient-3.0.jar
        commons-logging-1.0.4.jar
        jdom-1.0.jar
        jsr181-api.jar
        spring.jar
        wsdl4j-1.6.1.jar
        xfire-all-1.2.6.jar
        XmlSchema-1.4.7.jar
三、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>xfire</display-name>  
  <!-- 定義裝入的spring配置檔案 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
     classpath:org/codehaus/xfire/spring/xfire.xml
     /WEB-INF/xfire-servlet.xml
    </param-value>
  </context-param>
  <!-- 自動裝配配置資訊 -->
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 它主要負責處理由 JavaBean Introspector 功能而引起的快取洩露 -->
    <listener>
        <listener-class>
            org.springframework.web.util.IntrospectorCleanupListener
        </listener-class>
    </listener>
    
  <!-- 注意因為servlet-name為xfire,固xfire配置檔名應該是xfire-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>*.ws</url-pattern>
  </servlet-mapping>
  <!-- 配合Spring容器中XFire一起工作的Servlet- -->
  <servlet>
    <servlet-name>xfireServlet</servlet-name>
    <servlet-class>
    org.codehaus.xfire.spring.XFireSpringServlet
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>xfireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
</web-app>


四、xfire-servlet.xml
<?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" /> -->
    <bean id="baseExporter" class="org.codehaus.xfire.spring.remoting.XFireExporter"
        lazy-init="false" abstract="true">
        <property name="serviceFactory" ref="xfire.serviceFactory" />
        <property name="xfire" ref="xfire" />
        <property name="outHandlers">
            <list>
                <ref bean="domOutHandler"/>
                <ref bean="soapOutHandler"/>
            </list>
        </property>
        <property name="inHandlers">
            <list>
                <ref bean="domInHandler"/>
            </list>
        </property>
    </bean>

    <bean id="domOutHandler" class="org.codehaus.xfire.util.dom.DOMOutHandler" />
    <bean id="soapOutHandler" class="com.bing.webservice.SoapOutHandler" />
    <bean id="domInHandler" class="org.codehaus.xfire.util.dom.DOMInHandler" />
    
    <!-- basic configuration -->
    
    <!--  定義訪問的url,但是web.xml需要配置org.springframework.web.servlet.DispatcherServlet-->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
               <property name="urlMap">
                   <map>
                      <entry key="/hello.ws">
                          <ref bean="helloExporter"/>
                      </entry>
                   </map>
               </property>
    </bean>
<!-- hello webservice -->
     <bean id="helloExporter" parent="baseExporter">
        <property name="serviceBean" ref="hello" />
        <property name="serviceClass" value="com.bing.webservice.IHello" />
        <property name="name" value="sayHello"/>
    </bean>
    <bean id="hello" class="com.bing.webservice.HelloImpl" />
    <!-- hello webservice -->
</beans>


五、

介面
package com.bing.webservice;

public interface IHello {
    public String sayHello(String name);
}



實現類
package com.bing.webservice;
public class HelloImpl implements IHello {

    public String sayHello(String name) {
        return "hello! "+ name;
    }

}




攔截器
package com.bing.webservice;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;

public class SoapOutHandler extends AbstractHandler {

    public void invoke(MessageContext mc) throws Exception {
          System.out.println(mc.getCurrentMessage());
    }
}



客戶端測試
package com.bing.webservice;
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 TestClient {
    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:8080/xfire/services/sayHello";
          IHello ds= null;
          try {
           ds=(IHello)factory.create(serviceModel, serviceUrl);
          } catch (MalformedURLException e) {
           e.printStackTrace();
          }
          System.out.println(ds.sayHello("Tom"));
    }

}



另一種方式呼叫:

client.xml  放在src目錄下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
		<bean id ="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
		       <property name="serviceClass">
		           <value>com.bing.webservice.IHello</value>
		       </property>
		       <property name="wsdlDocumentUrl">
		           <value>
					http://localhost:8080/xfire/services/sayHello?wsdl
					</value>      
		       </property>
		</bean>
</beans>

TestClientByXml.java
package com.bing.webservice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestClientByXml {

    /**
     */
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("client.xml");
        IHello hello=(IHello)ac.getBean("testWebService");
        hello.sayHello("lubing");

    }

}