现在的位置: 首页 > 综合 > 正文

Spring+CXF整合

2014年05月16日 ⁄ 综合 ⁄ 共 2043字 ⁄ 字号 评论关闭

版本:Spring3.2.2

            CXF2.7.2

1.导入jar包

CXF的jar包

spring的jar包

2.配置web.xml,在web.xml中加入一下内容

    <!-- 加载Spring容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>	
	<!-- 设置Spring容器加载配置文件路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:cxf.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

3.配置cxf.xml,在src目录下新建cxf.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:lang="http://www.springframework.org/schema/lang"  
    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://www.springframework.org/schema/lang


http://www.springframework.org/schema/lang/spring-lang.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:endpoint id="HelloWorld"  implementor="cn.cxf.impl.HelloWorldImpl"  
         address="/hello">              
    </jaxws:endpoint>                                                     
</beans>       

4.编辑WebService接口类
package cn.cxf;


import javax.jws.WebService;


@WebService
public interface HelloWorld {
	String sayHi(String name);
}


5.编辑WebService实现类

package cn.cxf.impl;



import javax.jws.WebService;

import cn.cxf.HelloWorld;

@WebService(endpointInterface="cn.cxf.helloWorld",serviceName="HelloWorldImpl")
public class HelloWorldImpl implements HelloWorld {


	
	@Override
	public String sayHi(String name) {
		// TODO Auto-generated method stub
		return "i am superman";
	}


}

6.启动Tomcat,在浏览器中输入地址http://localhost:8080/CXFSpring/hello?wsdl,若成功则有以下内容

抱歉!评论已关闭.