Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3556584
  • 博文数量: 109
  • 博客积分: 10011
  • 博客等级: 上将
  • 技术积分: 2457
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-18 19:04
文章分类

全部博文(109)

文章存档

2011年(1)

2010年(10)

2009年(36)

2008年(62)

我的朋友

分类: Java

2010-06-19 15:30:30

CXF与spring集成

CXF与spring集成
http://blog.csdn.net/pengchua/archive/2008/07/30/2740572.aspx
1. 新建web project ,并加入apache-cxf-2.0.7\lib所有包,编写要发布的web service 接口和实现.这一步,与前面一样。
Java代码 复制代码
  1. import javax.jws.WebService;   
  2.   
  3. @WebService    
  4.   
  5. public interface HelloWorld {     
  6.   
  7.      public String sayHello(String text);     
  8.   
  9. }   
  10.   
  11. import javax.jws.WebService;     
  12.   
  13. @WebService(endpointInterface="test.HelloWorld")     
  14.   
  15. public class HelloWorldImpl implements HelloWorld {     
  16.   
  17.       public String sayHello(String text) {     
  18.   
  19.                   return "Hello" + text ;     
  20.   
  21.     }     
  22.   
  23.   }   


@WebService 注解表示是要发布的web 服务

2. 在spring-cxf.xml配置发布的web service

Java代码 复制代码
  1. "1.0" encoding="UTF-8"?>   
  2. ""  
  3.     xmlns:xsi=""  
  4.     xmlns:jaxws=""  
  5.     xsi:schemaLocation="   
  6. http://www.springframework.org/schema/beans /spring-beans.xsd   
  7. http://cxf.apache.org/jaxws ">   
  8.     <import resource="classpath:META-INF/cxf/cxf.xml" />    
  9.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />    
  10.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    
  11.     
  12.     "hello" class="test.HelloWorldImpl" />    
  13.     "helloWorld" implementor="#hello"    
  14.         address="/HelloWorld" />    
  15.     

注意:        address="/HelloWorld" />
id:指在spring配置的bean的ID.

Implementor:指明具体的实现类.

Address:指明这个web service的相对地址,

3.  配置web.xml文件:

Java代码 复制代码
  1. "1.0" encoding="UTF-8"?>    
  2. "2.4" xmlns=""    
  3.     xmlns:xsi=""    
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee      
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    
  6.         
  7.         contextConfigLocation     
  8.         classpath:spring-cxf.xml    
  9.         
  10.     
  11.         
  12.         class>    
  13.          org.springframework.web.context.ContextLoaderListener    
  14.         class>    
  15.         
  16.           
  17.         
  18.         CXFServlet    
  19.         class>    
  20.             org.apache.cxf.transport.servlet.CXFServlet     
  21.         class>    
  22.         1    
  23.         
  24.     
  25.         
  26.         CXFServlet    
  27.         /*    
  28.         
  29.    

4.部署到tomcat服务器,输入: HelloWorld?wsdl,将显示这个web service的wsdl.
注意:如果web.xml配置CXFServlet
        /ws/*
则访问地址为:ws/ HelloWorld?wsdl
5. 下面就开始创建一个客户端程序,访问这个web service, 同样新建java project ,并加入apache-cxf-2.0.7\lib所有包. 创建与具体webservice技术无关的业务接口HelloWorld.java

public interface HelloWorld { 

     public String sayHello(String text); 

}

6.配置spring-client.xml
Java代码 复制代码
  1. ""  
  2.     xmlns:xsi=""  
  3.     xmlns:jaxws=""  
  4.     xsi:schemaLocation="   
  5. http://www.springframework.org/schema/beans /spring-beans-2.0.xsd   
  6. http://cxf.apache.org/jaxws ">   
  7.     
  8.     "client" class="test.HelloWorld"  
  9.       factory-bean="clientFactory" factory-method="create"/>   
  10.       
  11.     "clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">   
  12.       "serviceClass" value="test.HelloWorld"/>   
  13.       "address" value=""/>   
  14.   
  15.        
  16.         
  17.   

7.测试:
Java代码 复制代码
  1. import org.springframework.context.ApplicationContext;   
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  3.     
  4. import test.HelloWorld;   
  5.     
  6. public class Test {     
  7.         
  8.     public static void main(String[] args) {     
  9.     
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext(     
  11.                 "spring-client.xml");     
  12.         HelloWorld client = (HelloWorld) ctx.getBean("client");     
  13.         String result = client.sayHello("你好!");     
  14.         System.out.println(result);     
  15.     }     
  16. }   
阅读(1406) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~