Spring 4.*和CXF3.*整合

来源:互联网 发布:mac air sd卡 windows 编辑:程序博客网 时间:2024/05/20 04:31

之前写过一个CXF整合spring,那是的CXF是2.2的,Spring是3.2的,前些日子去CXF官网下了个3.1的,用Spring4.3的整合,发现用旧版本配置的方式来配置新版的已不管用了,这两天又在网上搜了CXF3.0和Spring4的整合方式,原来使用的包都变了,这又重新整理一份,真麻烦。


接口

[java] view plain copy
  1. package test;  
  2.   
  3.   
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebService;  
  6.   
  7.   
  8. @WebService  
  9. public interface Hello {  
  10. public String say(@WebParam(name="name")String name);  
  11. }  

实现

[java] view plain copy
  1. package test;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. @WebService(endpointInterface="test.Hello")  
  6. public class HelloImp implements Hello {  
  7.   
  8.     public String say(String name) {  
  9.         return "hello,"+name;  
  10.     }  
  11.   
  12. }  

bean.xml(或applicationContext.xml)

[java] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"   
  5.        xmlns:jaxws="http://cxf.apache.org/jaxws"  
  6.        xsi:schemaLocation=" http://www.springframework.org/schema/beans    
  7.                             http://www.springframework.org/schema/beans/spring-beans.xsd   
  8.                             http://www.springframework.org/schema/context    
  9.                             http://www.springframework.org/schema/aop/spring-context.xsd  
  10.                             http://cxf.apache.org/jaxws   
  11.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  12.     <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  13.     <jaxws:endpoint id="abc" address="/cde" implementor="test.HelloImp"></jaxws:endpoint>  
  14.       
  15. </beans>  

web.xml

[java] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.          xmlns="http://xmlns.jcp.org/xml/ns/javaee"   
  4.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee   
  5.                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
  6.          id="WebApp_ID"   
  7.          version="3.1">  
  8.   <display-name>test</display-name>  
  9.   <welcome-file-list>  
  10.     <welcome-file>index.html</welcome-file>  
  11.     <welcome-file>index.htm</welcome-file>  
  12.     <welcome-file>index.jsp</welcome-file>  
  13.     <welcome-file>default.html</welcome-file>  
  14.     <welcome-file>default.htm</welcome-file>  
  15.     <welcome-file>default.jsp</welcome-file>  
  16.   </welcome-file-list>  
  17.     
  18.   <servlet>  
  19.    <servlet-name>CXFServlet</servlet-name>  
  20.    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  21.    <load-on-startup>1</load-on-startup>  
  22.   </servlet>  
  23.   <servlet-mapping>  
  24.    <servlet-name>CXFServlet</servlet-name>  
  25.    <url-pattern>/service/*</url-pattern>  
  26.   </servlet-mapping>  
  27.     
  28.   <listener>  
  29.    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  30.   </listener>  
  31.     
  32.   <context-param>  
  33.    <param-name>contextConfigLocation</param-name>  
  34.    <param-value>classpath:bean.xml</param-value>  
  35.   </context-param>  
  36.     
  37. </web-app>  


使用的包(缺一不可)



访问URL:http://localhost:8080/test/service/cde?wsdl


生成客户端命令:wsdl2java -client -p client -d e:/ http://localhost:8080/test/service/cde?wsdl

把生成的客户端放入到项目里,直接运行其中xxxxxxx_Client.Java类,即可看到运行结果。


原创粉丝点击