---
title: "XFire+Spring发布Web Service（二）"
author: "benniaolk"
date: "2009-12-10T21:14:51Z"
tags: []
categories: ["编程语言"]
slug: "xfire-spring-web-service"
draft: false
lastmod: "2009-12-10T21:17:55Z"
id: 545878
url: "https://dreava.iteye.com/blog/545878"
---

上接XFire+Spring发布Web Service（一）

**2.配置文件及发布**  
下面就可以配置Spring和XFire，以及Web.xml等配置文件了。XFire发布Web Service有两种方式，一种是利用XFire的导出器，一种是利用在发布接口和实现类的相应位置添加注释，这种注释的标准是由BEA提出来的——JSR181，两者各有利弊，利用导出器无需对原有代码进行修改；而利用第二种方法，不适用改造现有系统，似乎只适合新开发Web Services的项目。本文暂时只讲第一种方法。  
先看一下Web.xml文件的配置，重要代码添加了注释：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

    http://java.sun.com/xml/ns/j2ee/web-app\_2\_4.xsd"&gt;

    &lt;context-param&gt;

       &lt;!-- Spring配置文件 --&gt;

       &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;

       &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;

 

 

    &lt;/context-param&gt;

 

   

    &lt;listener&gt;

       &lt;listener-class&gt;

           org.springframework.web.context.ContextLoaderListener

       &lt;/listener-class&gt;

    &lt;/listener&gt;

    &lt;!-- 配合Spring容器中XFire一起工作的Servlet --&gt;

    &lt;servlet&gt;

       &lt;servlet-name&gt;xfireServlet&lt;/servlet-name&gt;

       &lt;servlet-class&gt;

           org.codehaus.xfire.spring.XFireSpringServlet

       &lt;/servlet-class&gt;

    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;

       &lt;servlet-name&gt;xfireServlet&lt;/servlet-name&gt;

       &lt;url-pattern&gt;/service/\*&lt;/url-pattern&gt;&lt;!-- 在这个URI下开放Web Service服务 --&gt;

    &lt;/servlet-mapping&gt;

&lt;/web-app&gt;

接着我们来看一看Spring的配置文件，相关处有注释：

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;

    &lt;!--引入XFire的预配置文件--&gt;

    &lt;import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /&gt;

    &lt;bean id="HelloServiceImpl" class="demo.HelloServiceImpl" /&gt;

 

    &lt;!-- 使用导出器导出Web Service --&gt;

    &lt;bean id="HelloService"

        class="org.codehaus.xfire.spring.remoting.XFireExporter"&gt;&lt;!-- XFire导出器 --&gt;

        &lt;!-- 引用XFire.xml中定义工作 --&gt;

        &lt;property name="serviceFactory" ref="xfire.serviceFactory" /&gt;

        &lt;!-- 引用XFire.xml中定义的XFire实例 --&gt;

        &lt;property name="xfire" ref="xfire" /&gt;

        &lt;!-- 业务服务Bean --&gt;

        &lt;property name="serviceBean" ref="HelloServiceImpl" /&gt;

        &lt;!-- 业务服务Bean的窄接口类 --&gt;

        &lt;property name="serviceClass"

            value="demo.IHelloService" /&gt;

        &lt;!-- Web Service名称 --&gt;

        &lt;property name="name" value="HelloServiceUT"/&gt;

    &lt;/bean&gt;

&lt;/beans&gt;

这样，将项目部署到tomcat下，启动容器，在浏览器中输入[http://localhost:8080/springxfire/service/HelloServiceUT?wsdl](http://localhost:8080/springxfire/service/HelloServiceUT?wsdl)，可看见该发布的Web Service的WSDL文件。

 

晕倒。。javaeye发文怎么限制这么严啊？稍微长一点还发不上来。。无奈，本来是一篇文章的，结果活生生被拆成三篇，刚好一节一篇，各位看的时候耐点心吧~~