`
wxlgzxx_1988
  • 浏览: 67834 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

CXF与Spring整合基础学习笔记

阅读更多

    首先,新建项目名为cxf_Spring,

拷贝所需jar包至lib目录,CXFspring整合需要准备如下jar包文件,目录结构的截图如附件中的1.jpg文件

 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">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<!-- 加载Spring容器配置 -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<!-- 设置Spring容器加载配置文件路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext**.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.springframework.web.util.IntrospectorCleanupListener
		</listener-class>
	</listener>

	<!-- CXF配置 -->
	<servlet>
		<servlet-name>CXFService</servlet-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFService</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

</web-app>

 

 

 

 

然后在src目录中,新建一个applicationContext-server.xml文件,文件内容如下:

 


 

下面开始写服务器端代码,实体操作bean的代码如下:

package com.wxl.bean;

public class User {

	private Integer id;
	private String name;
	private String email;
	private String address;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "id:" + this.id + " name:" + this.name + " email:" + this.email
				+ " address:" + this.address;
	}

}

 

 

 

首先定制服务器端的接口,代码如下:

package com.wxl.server.service;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import com.wxl.bean.User;

/**
 * 定制客户端请求WebService所需要的接口
 * 
 * @author wangxl
 * 
 */
@WebService
@SOAPBinding(style = Style.RPC)
public interface IComplexUserService {
	public User getUserByName(@WebParam(name = "name")
	String name);

	public void setUser(User user);

}

 

 

 

 

 

 下面编写WebService接口的实现类,代码如下:

 

 

package com.wxl.server.service.impl;

import java.util.Date;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import com.wxl.bean.User;
import com.wxl.server.service.IComplexUserService;

@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ComplexUserServiceImpl implements IComplexUserService {

	public User getUserByName(String name) {
		User user = new User();
		user.setId(new Date().getSeconds());
		user.setName(name);
		user.setEmail(name + "@yahoo.cn");
		user.setAddress("Chian");
		return user;
	}

	public void setUser(User user) {
		System.out.println("############Server setUser###########");
		System.out.println("setUser:" + user);
	}

}

 

 注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。

下面要在applicationContext-server.xml文件中添加如下配置:

 

<!-- 注入接口实现类 -->
	<bean id="userServiceBean"
		class="com.wxl.server.service.impl.ComplexUserServiceImpl" />
	
	<!-- 日志拦截器 -->
	<bean id="inMessageInterceptor"
		class="org.apache.cxf.interceptor.LoggingInInterceptor">
		<constructor-arg value="receive" />
	</bean>

	<bean id="outLoggingInterceptor"
		class="org.apache.cxf.interceptor.LoggingOutInterceptor" />

	<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
	<jaxws:server id="userService"
		serviceClass="com.wxl.server.service.IComplexUserService"
		address="/users">
		<jaxws:serviceBean>
			<!-- 要暴露的 bean 的引用 -->
			<ref bean="userServiceBean" />
		</jaxws:serviceBean>

		<jaxws:inInterceptors>
			<ref bean="inMessageInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
	</jaxws:server>

  

下面将项目部署到tomcat,启动tomcat服务器后,在浏览器中请求如下地址:

http://localhost:8088/cxf_Spring/users?wsdl

如果你能看到wsdlxml文件的内容,就说明你成功了,注意的是上面地址的users就是上面xml配置中的address的名称,是一一对应的。

 

 

下面编写客户端请求的代码,代码如下:

 

package com.wxl.client.test;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wxl.bean.User;
import com.wxl.server.service.IComplexUserService;

//client端java的实现
public class UserWSClient {

	public static void main(String[] args) {
		 new UserWSClient().WSClientTest();
	}

	public void WSClientTest() {
		// 调用WebService
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(IComplexUserService.class);
		factory.setAddress("http://localhost:8088/cxf_Spring/users");
		// 添加日志拦截器
		factory.getInInterceptors().add(new LoggingInInterceptor());// 请求的
		factory.getOutInterceptors().add(new LoggingOutInterceptor());// 发送出去的
		IComplexUserService service = (IComplexUserService) factory.create();
		System.out.println("#############JAxWs Client getUserByName##############");
		User user = service.getUserByName("wxl");
		System.out.println(user.toString());
		user.setAddress("China-Hangzhou");
		service.setUser(user);
	}

}

 

  在main函数中调用WSClientTest()方法,运行后可以在控制台中看到如下信息:

2012-2-28 16:23:12 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.server.wxl.com/}IComplexUserServiceService from class com.wxl.server.service.IComplexUserService
#############JAxWs Client getUserByName##############
2012-2-28 16:23:13 org.apache.cxf.services.IComplexUserServiceService.IComplexUserServicePort.IComplexUserService
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:8088/cxf_Spring/users
Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:getUserByName xmlns:ns1="http://service.server.wxl.com/"><name>wxl</name></ns1:getUserByName></soap:Body></soap:Envelope>
--------------------------------------
2012-2-28 16:23:13 org.apache.cxf.services.IComplexUserServiceService.IComplexUserServicePort.IComplexUserService
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[302], content-type=[text/xml;charset=UTF-8], Date=[Tue, 28 Feb 2012 08:23:13 GMT], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:getUserByNameResponse xmlns:ns1="http://service.server.wxl.com/"><return><address>Chian</address><email>wxl@yahoo.cn</email><id>13</id><name>wxl</name></return></ns1:getUserByNameResponse></soap:Body></soap:Envelope>
--------------------------------------
id:13 name:wxl email:wxl@yahoo.cn address:Chian
2012-2-28 16:23:13 org.apache.cxf.services.IComplexUserServiceService.IComplexUserServicePort.IComplexUserService
信息: Outbound Message
---------------------------
ID: 2
Address: http://localhost:8088/cxf_Spring/users
Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:setUser xmlns:ns1="http://service.server.wxl.com/"><arg0><address>China-Hangzhou</address><email>wxl@yahoo.cn</email><id>13</id><name>wxl</name></arg0></ns1:setUser></soap:Body></soap:Envelope>
--------------------------------------
2012-2-28 16:23:13 org.apache.cxf.services.IComplexUserServiceService.IComplexUserServicePort.IComplexUserService
信息: Inbound Message
----------------------------
ID: 2
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[195], content-type=[text/xml;charset=UTF-8], Date=[Tue, 28 Feb 2012 08:23:13 GMT], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:setUserResponse xmlns:ns1="http://service.server.wxl.com/"></ns1:setUserResponse></soap:Body></soap:Envelope>
--------------------------------------

 这个server端是通过Spring整合配置的,client端是我们手动访问发布的地址来实现访问。下面我们将Client端也通过Spring配置完成整合。

 

 

首先在src下增加applicationContext-client.xml配置文件,文件内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-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">

	<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" />

	<!-- client端的spring实现 -->
	<jaxws:client id="userWsClient"
		serviceClass="com.wxl.server.service.IComplexUserService"
		address="http://localhost:8088/cxf_Spring/users">
	</jaxws:client>

</beans>

  

 

 在客户端UserWSClient类中添加一个Spring实现的方法,请求代码如下:

 

public void springWsClientTest() {

		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext-client.xml");
		IComplexUserService service = ctx.getBean("userWsClient",
				IComplexUserService.class);

		System.out
				.println("#############Spring Client getUserByName##############");
		User user = service.getUserByName("wxl");
		System.out.println(user.toString());

		user.setAddress("China-Hangzhou");
		service.setUser(user);

	}

 在main函数中运行 springWsClientTest()方法后,结果如下:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
2012-2-28 16:25:20 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.server.wxl.com/}IComplexUserServiceService from class com.wxl.server.service.IComplexUserService
#############Spring Client getUserByName##############
id:20 name:wxl email:wxl@yahoo.cn address:Chian

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

相关推荐

    webservice(cxf)与spring整合源码+文档

    标题"webservice(cxf)与spring整合源码+文档"表明这是一个关于如何将CXF Web服务与Spring框架集成的学习资源。文档和源代码一起提供,使得学习者能够通过实践来理解这一过程。 描述中提到"webservice与spring整合...

    Apache_cxf_学习笔记

    CXF与Spring的集成是常见的实践,这使得服务的生命周期管理、依赖注入等变得更加便捷。通过Spring配置文件可以声明CXF服务 bean,同时也可以利用Spring的事务管理、数据访问等功能。 本学习笔记将继续深入探讨CXF的...

    cxf学习笔记之结合spring创建客户端

    在IT行业中,CXF是一个广泛使用的开源框架,用于构建和服务导向架构...通过这种方式,可以更加专注于业务逻辑,减少与基础设施相关的繁琐工作。对于开发者而言,熟悉这种结合方式对于提升开发效率和维护性至关重要。

    webservice+cxf基础笔记和视频,

    然后,当你开始使用CXF和Spring整合时,你将学习如何在Spring配置文件中声明和配置CXF服务,以及如何将服务接口实现类注入到Spring容器中。此外,你还会了解到如何通过CXF的端点发布功能将服务暴露出来,以及如何...

    Axis2,CXF版本学习笔记

    4. **Spring集成**:CXF与Spring框架的良好集成,使得服务的配置和管理更加灵活。 5. **安全性**:学习如何实现WS-Security和其他安全策略,确保Web服务的安全通信。 在深入学习这两个框架时,你可能还会关注如何...

    Apache-cxf-学习笔记.docx

    - **CXF与Spring集成例子**:CXF可以方便地与Spring框架集成,利用Spring的依赖注入特性,实现服务的松耦合和灵活配置。这包括在Spring配置文件中声明CXF服务和客户端,以及利用Spring的AOP功能进行事务管理等。 ...

    Apache cxf 学习笔记.pdf

    Spring框架与CXF的集成使得服务的生命周期管理更加方便。你可以将CXF服务定义为Spring Bean,并利用Spring的依赖注入特性。此外,Spring还提供了AOP(面向切面编程)功能,可以用来处理服务的事务、安全等跨切面关注...

    CXF的学习笔记

    有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。支持二进制和遗留协议:CXF 的设计是一种可插拨的...

    【webservice】Springboot整合CXF包括源码

    本文将深入探讨如何整合Spring Boot与CXF,以便利用Spring Boot的便捷性和CXF的强大Web服务功能。我们将通过源码分析,了解整合过程中的关键步骤和核心配置。 首先,Spring Boot是Spring框架的一种快速开发工具,它...

    Apache_cxf_学习笔记.docx

    ### Apache CXF 学习笔记知识点汇总 #### 一、CXF简介 ##### 1.1 CXF概述 - **背景介绍**:Apache CXF 是一个高性能、功能丰富的开源框架,用于构建和消费 Web 服务。它融合了 Celtix 和 XFire 两个开源项目的...

    Apache_CXF.zip

    2. **CXF与Spring的集成** "CXF+SPRING.doc"可能介绍了如何将Apache CXF与Spring框架结合使用。Spring框架在企业级Java应用中广泛应用,它可以提供依赖注入、AOP(面向切面编程)等功能。通过集成,开发者可以利用...

    CXF webservice初学笔记

    本笔记将探讨CXF的基本概念、如何使用CXF构建Webservice以及与之相关的技术栈,如Spring、Struts2等。 1. **CXF简介** - CXF(CXF Xfire + XWS)是两个项目的合并,提供了SOAP、RESTful、XML/HTTP、WS-*等协议的...

    CXF笔记

    【CXF笔记】是关于Apache CXF框架的深入学习记录,该笔记可能涵盖了CXF的基本概念、使用方法、源码解析以及与开发工具的结合应用。Apache CXF是一个开源服务框架,它允许开发者构建和部署多种Web服务。通过CXF,我们...

    Java+WebService利用(cxf)开发笔记.rar

    - **部署服务**:使用CXF的Servlet或Spring整合来部署服务。 - **创建客户端**:使用CXF的工具,如wsimport,生成客户端代理类,并调用服务。 4. **SOAP与RESTful的区别**: - SOAP是基于XML的消息协议,主要...

Global site tag (gtag.js) - Google Analytics