`
zhu_r_d
  • 浏览: 6635 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

cxf-2.7.3与spring3.0.7整合实例

 
阅读更多

项目中要使用cxf,参考网上资料做的一个cxf+spring的实例。

一、所需要的jar:




 

二、在web.xml填加以下配置

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
<!-- 设置Spring容器加载配置文件路径 -->
	<context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:applicationContext-server.xml</param-value> 
    </context-param> 
    
 <!-- 加载Spring容器配置 -->
    <listener> 
        <listener-class> 
         org.springframework.web.context.ContextLoaderListener
        </listener-class> 
    </listener> 
    
<!-- 配置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> 
 
   <!-- 所有来自/webservice/*的请求交给cxf处理 --> 
    <servlet-mapping> 
        <servlet-name>CXFServlet</servlet-name> 
        <url-pattern>/webservice/*</url-pattern>    
    </servlet-mapping>

</web-app>

 
 三、创建服务端webservice类

 

1、首先定制服务器端的接口

 

 

package com.ws.cxf.dao;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface IHelloWorld {
	@WebMethod
	public String sayHello(String name);
}

 2、服务器端的接口的实现类

 

package com.ws.cxf.daoImpl;

import com.ws.cxf.dao.IHelloWorld;

public class HelloWorldImpl implements IHelloWorld{

	public String sayHello(String name) {
		System.out.println("sayHello() is called");
		return name +" helloWorld";
	}
}

 四、创建spring配置文件

1、创建applicationContext-server.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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://cxf.apache.org/jaxws
 http://cxf.apache.org/schemas/jaxws.xsd">

	<!--CXF配置 -->
	<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" />

	<!--服务端发布的webservice 
		在spring中使用jaxws:endpoint元素来暴露Web Service 
		id:指在spring配置的bean的ID 
		Implementor:指明具体的实现类
		Address:指明这个web service的相对地址 -->
	<jaxws:endpoint id="helloWorld" implementor="com.ws.cxf.daoImpl.HelloWorldImpl"
		address="/HelloWorld" />
</beans>

 2、创建applicationContext-client.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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://cxf.apache.org/jaxws
 http://cxf.apache.org/schemas/jaxws.xsd">

	<!--CXF配置 -->
	<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" />


	<!--客户端调用的webservice -->
	<jaxws:client id="helloWorldClient"
		address="http://localhost:8080/cxfTest/webservice/HelloWorld"
		serviceClass="com.ws.cxf.dao.IHelloWorld" />

</beans>

 五、服务端测试类

package com.ws.cxf.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ws.cxf.dao.IHelloWorld;

public class Client {
	public static void main(String[] args) {
		  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml"); 
		  IHelloWorld helloWorld=(IHelloWorld)context.getBean("helloWorldClient");
		  System.out.println(helloWorld.sayHello("jim"));
	}
}

六、测试

1、访问以下地址验证服务端是否配制成功

http://localhost:8080/web工程名/webservice/HelloWorld?wsdl 查看是否发布成功并生成了wsdl文件

http://localhost:8080/web工程名/webservice  直接访问该地址可查看系统提供几个webservice服务

2、运行Client类输出 jim helloWorld  则客户端连接成功。

 

源代码:

 

  • 大小: 83.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics