日期:2014-05-16  浏览次数:20740 次

apache Cxf和spring的结合

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

利用ApacheCXF和Spring 编写一个webservice,这是从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配置一样。