---
title: "apache Cxf和spring的结合"
author: "zhaoImpulse"
date: "2012-08-24T18:35:45Z"
tags: []
categories: ["开源软件"]
slug: "apache-cxf-spring"
draft: false
lastmod: "2012-08-24T18:36:50Z"
id: 1662166
url: "https://zhaoimpulse.iteye.com/blog/1662166"
---

最近在学习使用apachecxf来做webService,在网上搜索了一下，大都是和spring结合，但都结合的不好。所以我研究了下，发现有更好的方式。

[利用ApacheCXF和Spring 编写一个webservice](http://blog.csdn.net/yippan/article/details/6624422)，这是从csdn看到的一篇入门教程，但发现里面使用了过多的类路径，比如

```
<jaxws:endpoint   
      id="helloWorld"   
      implementor="demo.spring.HelloWorldInterfaceImpl"   
      address="/HelloWorld" />  
```

  

 中的implementor，都是写死的，这对以后的维护都很不方便。所以我想可以用另一种方式，完全和spring结合。

 

大部分都和上面引用的文章一样，只有一些小改动。

 

```
package com.cxf;
public interface CxfService {

	public String sayHello();
}
```

 去掉annotation。

 

实现类

 

```
package com.cxf;

import javax.jws.WebService;

import org.springframework.stereotype.Service;

@WebService
@Service
public class CxfServiceImpl implements CxfService {

	@Override
	public String sayHello() {
		return "Hello";
	}

}
```

 

 

spring中的配置：

 

```
<jaxws:endpoint id="cxfService" implementor="#cxfServiceImpl"
		address="/cxfService" />
```

其中implementor="#cxfServiceImpl"中的'#'就表示要引用一个存在的bean。由于我们的CxfServiceImpl并没有设置bean名，则spring就会给它命名cxfServiceImpl，这样即可引用。

我觉得这样挺好的，不用写死类名，一切都跟sprin配置一样。