- <!-- SurveyService -->
- <bean id="surveyService" class="ws.cxf.impl.SurveyService">
- <property name="excludeName" value="Michael"/>
- <property name="leastPonit" value="10"/>
- </bean>
最后,将定义的 Bean 暴露出去成为 Web Service 服务,通过 CXF 提供的 Schema 标签配置 <jaxws:server> ,这样定义的配置显得更加简洁与方便,定义如下:
- <!-- Expose SurveyWebService -->
- <jaxws:server id="surveyWebService"
- serviceClass="ws.cxf.ISurveyService"
- address="/SurveyWebService">
- <jaxws:serviceBean>
- <ref bean="surveyService"/> <!-- 要暴露的 bean 的引用 -->
- </jaxws:serviceBean>
- </jaxws:server>
在配置中,serviceClass 的值是我们的接口类的名称,address 为将要暴露出去的 Web Service 访问地址。比如:/SurveyWebService,那么客户端消费 Web Service 的地址就会成为 http://host:port/WebAPPName/SurveyWebService ,与之相应的 WSDL 地址则为: http://host:port/WebAPPName/SurveyWebService?wsdl 。
Web 应用配置
由于我们的示例是需要通过 Servlet 容器进行服务暴露,因此需要配置相对应的 web.xml 文件,首先是增加 Spring 的配置文件加载 Listener,如下:
- <!-- Spring Config Location -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/beanRefServer.xml</param-value>
- </context-param>
- <!-- Spring ContextLoaderListener -->
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
接下来配置 CXF Servlet 的定义,以及它的映射,如下:
- <!-- Apache CXFServlet -->
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <display-name>CXF Servlet</display-name>
- <servlet-class>
- org.apache.cxf.transport.servlet.CXFServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!-- CXFServlet Mapping -->
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
我们将之映射为 /* 。这样,服务端的代码与配置就全部完成了,接下来就是将应用程序部署到 Web 容器中去,并验证服务是否正常发布。
应用部署
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="F:/JavaProject/WebService/CXF/CXF_Spring_Survey"/>
docBase 里的内容根据您的实际项目所在的目录进行更改,注意使用 / 而不是 \ 即可。
启动服务
这时开始启动 Tomcat ,在启动的过程中,可以在启动窗口上看到以链接方式部署的应用在启动中会打印出一些相关信息来,最后显示启动成功。 通过访问 http://localhost:8080/CXF_Spring_Survey/ 可以看到 CXF 暴露的服务链接:
图 7. CXF 暴露的服务链接的内容示意图

可以直接点击进去,或者手工输入 WSDL 的地址进行访问:http://localhost:8080/CXF_Spring_Survey/SurveyWebService?wsdl ,可以看到如下的 WSDL 内容:
图 8. SurveyWebService 的 WSDL 内容示意图

这样,我们可以确定我们的服务真正发布成功了,接下来就可以利用客户端进行消费了。
消费服务
回到 Eclipse 开发平台,开始编写消费服务相关的代码,首先通过 Spring 与 CXF 的配置来定义 Web Service 的客户端 Bean,在 src 目录下创建 beanRefClient.xml 配置文件,同样,我们也需要引入 Spring 与 CXF 命名空间的声明,并引入 CXF 的 Bean 的定义文件,最后通过与服务端配置相对的 CXF 标签 <jaxws:client> 来定义客户端访问服务的声明,完整的定义内容如下:
- <?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://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <!-- Import Apache CXF Bean Definition -->
- <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"/>
- <!-- SurveyWebService Client -->
- <jaxws:client id="surveyServiceClient"
- serviceClass="ws.cxf.ISurveyService"
- address="http://localhost:8080/CXF_Spring_Survey/SurveyWebService"/>
- </beans>
定义说明:id 为 Spring 定义的 id,用来在程序里进行获取它的标识,serviceClass 仍是为服务端定义的接口类,address 为完整的 Web Service 地址,这个与服务端的定义不一样。
定义完配置文件,接下来我们编写访问的具体代码,在 test 目录下创建 ws.cxf.client 包,然后创建 SurveyServiceClient.java,完整的代码如下:
- package ws.cxf.client;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import ws.cxf.ISurveyService;
- public class SurveyServiceClient
- {
- public static void main(String[] args)
- {
- // 加载客户端的配置定义
- ApplicationContext context = new
- ClassPathXmlApplicationContext("beanRefClient.xml");
- // 获取定义的 Web Service Bean
- ISurveyService surveyService =
- (ISurveyService)context.getBean("surveyServiceClient");
- // 1、定义调查投票的变量与内容,用来发送给服务
- String username = "Test";
- int point = 88;
- // 调用方法进行服务消费
- String result = surveyService.vote(username,point);
- System.out.println("Result:" + result);
- // 2、传递不一样的调查投票内容
- username = "Michael";
- point = 100;
- // 再次调用方法进行服务消费,得到不一样的结果
- result = surveyService.vote(username,point);
- System.out.println("Result:" + result);
- // 3、第三次传递与调用
- username = "Jordan";
- point = 9;
- result = surveyService.vote(username,point);
- System.out.println("Result:" + result);
- }
- }
直接运行以上客户端消费程序,一共调用了三次 Web Service,并得到结果如下:
Result: 谢谢您的投票!您的投票分数通过审核!
Result: 您不能重复进行投票!
Result: 谢谢您的投票!您的投票分数太低!
本人开发环境为:MyEclipse6.5+jdk1.6+apache-tomcat-6.0.20+apache-cxf-2.3.3
开发原码于附件中下载,不包含JAR包.
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2012年资料/7月/7日/CXF+Spring开发环境搭建小试
