1.写一个接口和它的实现类,这就是我们想要发布的服务
package test; 

public interface Echo { 
public String echo(String in); 


package test; 

public class EchoImpl implements Echo { 
public String echo(String in) { 
return in; 


2.把服务通过spring和servlet容器发布出去
WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value> 
classpath:applicationContext-test.xml,classpath:org/codehaus/xfire/spring/xfire.xml
</param-value> 
</context-param> 
<listener> 
<listener-class> 
org.springframework.web.context.ContextLoaderListener
</listener-class> 
</listener> 
<servlet> 
        
<servlet-name>test</servlet-name> 
        
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    
</servlet> 

    
<servlet-mapping> 
        
<servlet-name>test</servlet-name> 
        
<url-pattern>/*</url-pattern> 
    
</servlet-mapping> 
</web-app> 
WEB-INF/classes/applicationContext-test.xml
<?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="echo" class="test.EchoImpl"/> 
</beans> 
WEB-INF/test-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> 
<bean  
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
<property name="urlMap"> 
<map> 
<entry key="/echoService"> 
<ref bean="echoService"/> 
</entry> 
</map> 
</property> 
</bean> 
<bean id="echoService" class="org.codehaus.xfire.spring.remoting.XFireExporter"> 
<property name="serviceFactory"> 
<ref bean="xfire.serviceFactory"/> 
</property> 
<property name="xfire"> 
<ref bean="xfire"/> 
</property> 
<property name="serviceBean"> 
<ref bean="echo"/> 
</property> 
<property name="serviceClass"> 
<value>test.Echo</value> 
</property> 
</bean> 
</beans> 
通过http://localhost:8080/test/echoService就可以访问这个service

3.客户端
package test; 

import java.net.MalformedURLException; 

import org.codehaus.xfire.client.XFireProxyFactory; 
import org.codehaus.xfire.service.Service; 
import org.codehaus.xfire.service.binding.ObjectServiceFactory; 


public class XFireClient { 
public static void main(String[] args) { 

Service serviceModel 
= new ObjectServiceFactory().create(Echo.class); 
try { 

Echo service 
= (Echo) new XFireProxyFactory().create(serviceModel, 
"http://localhost:8080/test/echoService"); 
System.out.println(service.echo(
"hello,world")); 
catch (MalformedURLException e) { 
e.printStackTrace(); 




注意:第一次调用很慢,以后调用比第一次快很多