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

这个比较迷惑人。。。至少对我这个初学者来说是如此。后面解释原因。
一、加入cxf支持
简单的说就是创建一个普通的web项目,并加将cxf的发布包和依赖包加入到项目的lib目录下。如果本项目不存在对外提供web服务的义务,那么可以只加入作为客户端所需的依赖包即可(具体是哪几个,我现在也不知道 :wink: )

二、创建客户端的对应接口

package cn.ibeans.cxf.client;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
String sayHello(String msg);
}


三、配置spring

<?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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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" />
-->

<bean id="client" class="cn.ibeans.cxf.client.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>

<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="cn.ibeans.cxf.client.HelloWorld"/>
<property name="address" value="http://localhost:8088/cxfServer/HelloWorld"/>
</bean>
</beans>


四、测试一把
写个测试类

package cn.ibeans.cxf.client;

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

public class HelloWorldTest {

/**
* @param args
*/
public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // your Spring ApplicationContext
HelloWorld client = (HelloWorld) context.getBean("client");
String s = "aaaaaa";
if(client!=null){
s = client.sayHello("1234567880");
}
System.out.println("服务端的返回值为:"+s);
}

}


运行一下测试类。看看都输出了什么?

五、小结
1、一定不要忘了在客户端创建一个名称和方法均与服务端一至的接口,这是我刚明白的道理。网上很多文章都写得很好。但全都忽略了这一点。那些文章往往将客户端和服务端放在一个web项目里演示。所以客户端可以直接引用服务端的接口类。但现实中完全不是这样,服务端能对客户端提供的只有wsdl的访问地址。很是感慨:高手写的文章往往会忽略一些不值一提,但对初学者却非常重要的细节。我就在这点上花费了不少时间才明白。

2、当前例子中只写了个java类来做测试。真正的web应用会对它页面来调用接口实现与服务端的对接。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值