---
title: "配置Xfire+Spring+Ibatis心得"
author: "rocye"
date: "2009-02-17T09:31:42Z"
tags: []
categories: ["企业架构"]
slug: "xfire-spring-ibatis"
draft: false
lastmod: "2009-03-25T10:22:20Z"
id: 333916
url: "https://rocye.iteye.com/blog/333916"
---

**配置Xfire+Spring+Ibatis心得**

       最近开始做webservice针对Spring+Struts+Ibatis+Ajax这样一套底层架构项目的集成，并以Flex为客户进行测试，中间碰到一些问题也得到一些知识想记录一下。

 

◆现存项目核心架构: Ajax+Struts+Spring+Ibatis

◆集成框架：websevice 中的Xfire1.2.6

◆前端客户端：Flex

 

1．首先在Xfire官网下载Xfire相关jar包，将xfire-all-1.2.6.jar以及xfire-distribution-1.2.6.zip下的lib文件夹所有在项目中没有的包均导入到项目中。（本人曾经因为怕全部导入会导致有包不会被用到，所以想通过启动项目根据它所报的错来逐个导入，接连重启服务十来次，真的是很浪费时间，最后将lib下的所有包都导入才不会报找不到Class那个错）

 

2．大多数资料都有介绍过Xfire与Spring的结合，无外两种，一种是使用导出器导出Web Service，另一种是采用JSR 181进行注释式导出。很显然用导出器结构很清楚，但对于每一个需要导出的业务都要为其配置一个导出器，而JSR 181试采用注释的方式，配置起来更简单，可想而知，我会首先想到用JSR 181方式进行本项目的webservice集成测试。

 

3．现存的项目Struts和Spring及Ibatis联系的非常密切，从底层的Dao，业务层的Service，到控制层的Action均交给Srping来管理，项目有三类配置文件：web.xml(这里不做详述)、spring-config.xml 主要用来放Dao层，业务层及其事务的配置；struts-config.xml 主要用来声明所有用到的Action类；action-severlet.xml 主要用来连接 Action及业务类。起初根本不知道切和点，甚至会想到webservice向外导出时定义的窄接口会是在action这一层，其实这怎么可能呢，既然现在由webservice向外界提供访问接口，那么根本再不需要action，不论它以前与spring有多大关联，这里都可以将其全部Cut掉，可以说以前的action相当于一个websevice应用，而现在要将其改成更标准的Xfire。知道这一点后就简单了。

 

4．很显然Spring 中所配置的Dao层和业务层均可不变，所谓的向外的窄接口就是先前的业务层接口了，那么采用JSR 181就会这样：

@WebService(serviceName = "MsgunitService", //①指定导出的Web Service名称

              endpointInterface = "org.sh.spiss.eme.msgunit.MsgunitService")      //②对应的窄接口

**public** **class** MsgunitServiceImpl **implements** MsgunitService{

      

       **private** MsgunitDao msgunitDaoImpl;

      

       **public** MsgunitDao getMsgunitDaoImpl() {

              **return** msgunitDaoImpl;

       }

 

       **public** **void** setMsgunitDaoImpl(MsgunitDao msgunitDaoImpl) {

              **this**.msgunitDaoImpl = msgunitDaoImpl;

       }

      

       **public** **int** deleteMsgunit(Msgunit msgunit) {

              **return** getMsgunitDaoImpl().deleteMsgunit(msgunit);

       }

      

       **public** Msgunit getMsgunit(Msgunit msgunit) {

              **return** getMsgunitDaoImpl().getMsgunit(msgunit);

       }

 

       **public** List getMsgunitList(Msgunit msgunit) {

              **return** getMsgunitDaoImpl().getMsgunitList(msgunit);

       }

 

       **public** **int** getTotalMsgunit(Msgunit msgunit) {

              **return** getMsgunitDaoImpl().getTotalMsgunit(msgunit);

       }

 

       **public** **int** insertMsgunit(Msgunit msgunit) {

              **return** getMsgunitDaoImpl().insertMsgunit(msgunit);

       }

 

       **public** **int** updateMsgunit(Msgunit msgunit) {

              **return** getMsgunitDaoImpl().updateMsgunit(msgunit);

       }

}

对应的接口也需要注释:

@WebService(targetNamespace = "http://www.xxxx.com") //①指定SOAP的命名空间

**public** **interface** MsgunitService {

      

      

       **public** **int** deleteMsgunit(Msgunit msgunit);

      

       **public** Msgunit getMsgunit(Msgunit msgunit);

      

       **public** List getMsgunitList(Msgunit msgunit);

 

       **public** **int** getTotalMsgunit(Msgunit msgunit);

 

       **public** **int** insertMsgunit(Msgunit msgunit);

 

       **public** **int** updateMsgunit(Msgunit msgunit);

      

}

接下来就需要进行相关配置文件的编写了;

 

&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.xsd"&gt;

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

 

       &lt;bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" /&gt;

①该Bean获取Spring容器中所有标注@WebService的Bean

       &lt;bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping"&gt;

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

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

       &lt;/bean&gt;

②对标注@WebService的Bean进行处理，完成导出工作