CXF+Spring 整合

 


运行环境

JDK 1.7

tomcat 7.0

CXF: apache-cxf-2.7.16.zip(下载地址:http://mirrors.cnnic.cn/apache/cxf/2.7.16/apache-cxf-2.7.16.zip

Spring 3.0.4

IDE:eclips

 

读者这里需要注意的一点事 CXF 2.0+版本和CXF 3.0+版本的配置有些差异,这里使用的是apache-cxf-2.7.16.zip,且测试通过。

 

首先

创建 一个工程

 

第二  导入所需要jar包  CXF+Spring  会有很多jar, 这里我们只要将 所需要的jar包导入就可以了。

 

第三 修改 web.xml 文件

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>keyManage</display-name> 
  <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>  
        <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/*</url-pattern>  
  </servlet-mapping>
  
  
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


这里需要注意的一点是 我的applicationContext.xml是放在spring的默认目录 WEB-INF 下,所以 没有知道 applicationContext.xml的文件路径。如果你将applicationContext.xml放在src目录下 请 指定文件路径,加上

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>

 

第四  创建 对外暴漏的接口

package keyManage;

import javax.jws.WebService;

@WebService 
public interface UserService { 
   public String getName(String userName); 
} 

 

第五  创建接口实现(这里是你具体的业务逻辑,可以进行 数据库和具体实现等处理)

package keyManage;


import javax.jws.WebService;

import keyManage.UserService;

@WebService(endpointInterface = "keyManage.UserService") 
public class UserServiceImpl implements UserService { 

   public String getName(String userName){
       return "Hello " + userName; 
   } 
} 


 

 

 

第六  创建 WEB-INF/applicationContext.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:endpoint id="userService"
		implementor="keyManage.UserServiceImpl" 
		address="/UserService" />
</beans> 


 

 到这里,CXF的服务器端 开发完毕。

 

在 浏览器中输入 http://localhost:8080/keyManage/UserService?wsdl

 

出现下图 说明测试通过。

 

 

最后是 客户端代码

 

package keyManage;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import keyManage.UserService;

public class TestGreetingService {
	public static void main(String[] args) {
		//创建WebService客户端代理工厂
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		//注册WebService接口
		factory.setServiceClass(UserService.class);
		//设置WebService地址
		factory.setAddress("http://localhost:8080/keyManage/UserService");
		UserService userService = (UserService)factory.create();
		System.out.println("invoke webservice...");
		System.out.println(userService.getName("gary"));   
	}
}

 

运行 得到结果

 


END

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值