cxf+spring 的自己学习心得

来源:互联网 发布:Ubuntu gnu grub 编辑:程序博客网 时间:2024/06/01 22:17

cxf+spring编写webservice

一 、Cxf jar 下载链接http://cxf.apache.org/download.html

 

首先 创建web项目 

将 解压后 lib文件夹下边的jar加载到项目中  在web.xml文件中配置cxf  代码如下

   <servlet>  

     <servlet-name>CXFServlet</servlet-name>  

    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  

     <load-on-startup>1</load-on-startup>  

   </servlet>  

   <servlet-mapping>  

     <servlet-name>CXFServlet</servlet-name>  

     <url-pattern>/services/*</url-pattern>  

   </servlet-mapping>  

 

二、一些相应的解释说明

CXF中采用注解的方式声明哪些类作为WebService进行发布, 

   1、  @webService :声明webservice接口

   2@WebService(endpointInterface="com.test.TestService"):声明这个类是TestService接口的实现类。

 

其次 在WEB-INF下创建cxf-servlet-xml文件 代码如下

<?xml version="1.0" encoding="UTF-8"?>  

   

 <beans xmlns="http://www.springframework.org/schema/beans"  

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

     xmlns:jaxws="http://cxf.apache.org/jaxws"  

     xsi:schemaLocation="  

         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  

         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  

           

     <import resource="classpath:META-INF/cxf/cxf.xml" />  

     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  

     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  

     <jaxws:server id="testService" serviceClass="com.test.TestService" address="/testService">  

     <jaxws:serviceBean>  

        <bean class="com.test.TestServiceImpl" />  

    </jaxws:serviceBean>  

  </jaxws:server>  

 </beans>  

 

其中 id是自定义  serviceClass是接口的完整类名  address 此接口的访问路径  <jaxws:serviceBean/>标签用于配置接口的实现类

 然后,在web.xml中配置启动时加载cxf-servlet.xml,并配置Spring的监听器,代码如下:

 

    <!-- 配置文件cxf-servlet.xml 使这个文件在项目启动的时能够被加载 -->

    <context-param>  

     <param-name>contextConfigLocation</param-name>  

     <param-value>/WEB-INF/cxf-servlet.xml</param-value>  

   </context-param>  

     <!-- 配置监听 -->

   <listener>  

     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

   </listener>  

最后,发布到tomcat中并启动,在浏览器中输入http://localhost:8080/cxftest/services/testService?wsdl,发布成功!


0 0