xFire与Spring结合例子

本文介绍如何将XFire与Spring框架集成,实现基于Tomcat的WebService应用。通过配置web.xml和xfire-servlet.xml文件,部署一个简单的EchoService服务。

XFire 1.2.6

 

官方网 上下载xfire-distribution-1.2.6.zip,下载到本地解压后可以得到XFire编译打包后的文件xfire-all-1.2.6.jar,lib目录下为XFire所依赖的第三方Jar文件,在开发的时候用得到,此外还包括手册和几个例子。建议在Eclipse里建一个用户库,把 xfire-all-1.2.6.jar和XFire所依赖的第三方Jar文件都包含进去。

 

建立一个Tomcat工程,名叫XFireSpring。--用里面spring的例子

 

WEB-INF文件夹下web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- START SNIPPET: xfire -->
	<!--xfire使用 classpath:org/codehaus/xfire/spring/xfire.xml-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
        <!--classpath:org/codehaus/xfire/spring/xfire.xml  -->
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

	<!-- 配合Spring容器中XFire一起工作的Servlet-->
    <servlet>
        <servlet-name>xfire</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>xfire</servlet-name>
        <url-pattern>/services/*</url-pattern><!-- 在这个URI下开放Web Service服务 -->
    </servlet-mapping>
<!-- END SNIPPET: xfire -->
</web-app>

 

WEB-INF文件夹下xfire-servlet.xml :

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!-- 引入XFire预配置信息 -->
	<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

    <!-- START SNIPPET: xfire -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/EchoService"><!-- 访问路径http://127.0.0.1:8080/XFireSpring/services/EchoService?wsdl -->
                    <ref bean="echo"/>
                </entry>
            </map>
        </property>
    </bean>

    <!-- Declare a parent bean with all properties common to both services -->
    <bean id="echo" class="org.codehaus.xfire.spring.remoting.XFireExporter">
    	<!-- 引用xfire.xml中定义的工厂 -->
        <property name="serviceFactory">
            <ref bean="xfire.serviceFactory"/>
        </property>
        <!-- 引用xfire.xml中的xfire实例 -->
        <property name="xfire">
            <ref bean="xfire"/>
        </property>
        <property name="serviceBean">
            <ref bean="echoBean"/>
        </property>
        <property name="serviceClass">
            <value>org.codehaus.xfire.spring.example.Echo</value>
        </property>
    </bean>

    <bean id="echoBean" class="org.codehaus.xfire.spring.example.EchoImpl"/>

    <!-- END SNIPPET: xfire -->
</beans>

具体详看附件

 

http://127.0.0.1:8080/ XFireSpring/services/EchoService?wsdl

package com.xfire.core.client; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.xfire.core.entity.UserInfo; import com.xfire.core.service.IUserInfoService; /** *@author jilongliang *@Date 2012-3-5 * */ public class UserInfoClient { public static void main(String[] args) { getServiceList(); setServiceList(); } static String url = "http://localhost:8081/xfire/services/UserInfo"; /** * */ public static void getServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List list = (ArrayList) iAddressService .getAddressList(); System.out.println("一共多少条数据:" + list.size()); for (Iterator iter = list.iterator(); iter.hasNext();) { UserInfo a = iter.next(); System.out.println(a); } } catch (MalformedURLException e) { e.printStackTrace(); } } public static void setServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List listAdd = new ArrayList(); UserInfo address = new UserInfo(); address.setIdentifier(1); address.setCountry("中國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress("廣東陽春"); address.setPostCode("1111111"); address.setExist(false); address.setArrary(new String[] { "22", "23", "24" }); listAdd.add(address); address.setIdentifier(2); address.setCountry("中國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值