---
title: "CXF + Spring 3.0的学习例子"
author: "devon.k"
date: "2011-07-31T14:25:11Z"
tags: []
categories: ["编程语言"]
slug: "cxf-spring-3-0"
draft: false
lastmod: "2011-07-31T15:30:35Z"
id: 1136650
url: "https://devon-k.iteye.com/blog/1136650"
---

创建一个Web Project
 编写Web Service代码
 启动Web Service服务
 创建一个Java application project
 编写客户端代码
 客户端访问Web Service服务


**创建一个Web Project**
这里我选用Maven来创建，当然你也可以用其他方式来创建

```DOS

mvn archetype:create -DgroupId=com.wstest -DartifactId=wstest -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp

```

创建好之后，我们给工程添加Spring 3.0 和 cxf 2.4.1的jar包的dependency，由于dependency信息在pom.xml中比较长，所以就不帖出来了，可以在附件中找到。

接着执行如下命令，以生成Eclipse工程配置文件

```Dos

mvn -DdownloadSources=true eclipse:clean eclipse:eclipse -Dwtpversion=1.5

```

将工程导入Eclipse，我们就可以开始编码了。

**编写Web Service代码**
首先New一个对外暴露的接口HelloWorld.java

```java

package com.wstest.service;

import javax.jws.WebService;

@WebService
public interface HelloWorld {

	public String sayHello(String text);
}

```


接着创建一个接口的实现类HelloWorldImpl.java

```java

package com.wstest.service.impl;

import javax.jws.WebService;

import com.wstest.service.HelloWorld;

@WebService
public class HelloWorldImpl implements HelloWorld {

	@Override
	public String sayHello(String text) {
		return "Hello, I am Robot." + text;
	}
}

```


最后我们创建一个MainServer类，用来启动我们的Web Service服务

```java

package com.wstest.server;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.wstest.service.impl.HelloWorldImpl;

public class MainServer {

	public static void main(String[] args) {
		
		JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
		factory.setServiceClass(HelloWorldImpl.class);
		factory.setAddress("http://localhost:8080/HelloWorld");
		// 注意别忘了添加service bean
		HelloWorldImpl hello = new HelloWorldImpl();
		factory.setServiceBean(hello);
		
		Server server = factory.create();
		server.start();
	}
}

```


**启动Web Service服务**
执行MainServer启动Web Service服务，就可以打开浏览器查看wsdl信息了，输入http://localhost:8080/HelloWorld?wsdl

**创建一个Java application project**

```DOS

mvn archetype:create -DgroupId=com.wsclient -DartifactId=wsclient -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart

```

创建好之后，我们给工程添加Spring 3.0 和 cxf 2.4.1的jar包的dependency，dependency信息可以在附件中找到。

执行如下命令，以生成Eclipse工程配置文件

```Dos

mvn -DdownloadSources=true eclipse:clean eclipse:eclipse

```

将工程导入Eclipse

**编写客户端代码**
创建一个Web Service暴露接口类，HelloWorld.java(类名，包名都一样)

```java

package com.wstest.service;

import javax.jws.WebService;

@WebService
public interface HelloWorld {

	public String sayHello(String text);

}

```


接着创建一个Client类

```java

package com.wsclient.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.wstest.service.HelloWorld;

public class Client {

	public static void main(String[] args) {

		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress("http://localhost:8080/HelloWorld");
		factory.setServiceClass(HelloWorld.class);

		HelloWorld helloWorld = (HelloWorld) factory.create();
		System.out.println(helloWorld.sayHello("How do you do."));
	}
}

```


**客户端访问Web Service服务**
在Web Service服务已被启动的前提下，直接运行Client类，就好了。
输出结果

```text

Hello, I am Robot.How do you do.

```