在javaeye里参考大牛们的帖子去配置spring2.5+hibernate3.2+CXF2.1,最终解决了路径问题后,终于成功了!
CXF是Apache的一个重点项目,终于放出来了,因为它跟spring的结合很方便,于是就用一下,还是要自己动手一步步弄一下,才知道其配置上的一些小细节(如果用插件的话,就感觉不到它跟spring的关系和不知道它的很多细节,所以推荐不要用插件的好)。
在 这里顺带提一下,spring2.5结合junit4.4可以很容易地运用annotation来进行testcase的编写,不过要注意的是 eclipse3.3或者eclipose3.4,里头自带的junit4是junit4.3版本的,缺少需要的方法,所以要去下载最新的 junit4.4版,然后替换掉eclipse插件里的junit.jar包。
准备依赖包,依赖包不用想那么多,我在这里是把握的项目包弄个截图,所以是很多的,重点是spring,hibernate,CXF*这些包。
在web.xml文件中添加以下servlet配置:
<!-- CXF 配置 -->
< servlet >
< servlet-name > CXFServlet </ servlet-name >
< servlet-class > org.apache.cxf.transport.servlet.CXFServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > CXFServlet </ servlet-name >
< url-pattern > /services/* </ url-pattern >
</ servlet-mapping >
添加applicationContext-cxf.xml文件到上下文,配置如下:
<? 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:jaxws ="http://cxf.apache.org/jaxws"
xsi:schemaLocation ="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-autowire ="byName" default-lazy-init ="true" >
< description > 基于Apache CXF的Web Service配置文件 </ description >
< import resource ="classpath:META-INF/cxf/cxf.xml" />
< import resource ="classpath:META-INF/cxf/cxf-servlet.xml" />
< import resource ="classpath:META-INF/cxf/cxf-extension-soap.xml" />
< import
resource ="classpath:META-INF/cxf/cxf-extension-javascript-client.xml" />
< bean id ="helloWorldImpl" class ="com.lbg.ws.test.impl.HelloWorld" />
< jaxws:endpoint id ="helloWorld" implementor ="#helloWorldImpl"
address ="/HelloWorld" />
</ beans >
建立webservice的接口与实现:
package com.lbg.ws.test;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService(name = " HelloWorld " )
public interface IHelloWorld {
@WebMethod(operationName = " sayHello " )
public String sayHello();
}