zoukankan      html  css  js  c++  java
  • Spring集成XFire开发WebService

    Spring是眼下最流行的JavaEE Framework,可是使用Spring的Spring-WS开发WebService却十分繁琐。XFire是一个简化WebService开发的开源项目。通过Spring和XFire的结合能够大大简化基于Spring Framework的应用中的WebService开发。

    Spring和XFire能够通过多种方式结合,下文介绍的是笔者经常使用的一种简单而有用的方法。

    所用的Spring版本号为2.0,XFire版本号为1.2.6

    1、配置XFire Servlet
    在web.xml中增加例如以下配置:


    <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>
    org.codehaus.xfire.spring.XFireSpringServlet
    </servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/servlet/XFireServlet/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    2 配置Spring的监听器,同基于spring的Web项目一样Spring的监听器是不可缺少的。


    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:org/codehaus/xfire/spring/xfire.xml,
    /WEB-INF/applicationContext.xml
    </param-value>
    </context-param>
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>

    下面是完整的web.xml配置文件


    <?xml version="1.0" encoding="UTF-8"?>
    <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
    [url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]">
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:org/codehaus/xfire/spring/xfire.xml,
    /WEB-INF/applicationContext.xml
    </param-value>
    </context-param>
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>
    org.codehaus.xfire.spring.XFireSpringServlet
    </servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/servlet/XFireServlet/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    </web-app>

    3 定义接口及实现服务

    定义接口。这个接口中定义要通过WebService暴露的方法


    package org.ccsoft;

    publicinterface HelloWS {
    public String sayHello(String sb);
    }

    实现服务


    package org.ccsoft;

    publicclass HelloWSImp implements HelloWS {
    public String sayHello(String sb) {
    // TODO Auto-generated method stub
    return"Hello "+sb;
    }
    }

    4 配置服务

    将上文中实现的服务,增加到spring的配置文件里。


    <?xml version="1.0" encoding="UTF-8"?


    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    [url]http://www.springframework.org/schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]">

    <bean id="helloWS" class="org.ccsoft.HelloWSImp"/>
    <bean name="helloService" class="org.codehaus.xfire.spring.ServiceBean">
    <property name="serviceBean" ref="helloWS"/>
    <property name="serviceClass" value="org.ccsoft.HelloWS"/>
    <property name="inHandlers">
    <list>
    <ref bean="addressingHandler"/>
    </list>
    </property>
    </bean>

    <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>
    </beans>

  • 相关阅读:
    1051 高度检查器
    Word+Excel 问题及解决
    Python——面向对象、绑定对象、组合
    Python——异常处理
    Python——包
    Python——模块
    Python——序列化模块
    Python——collections模块、time模块、random模块、os模块、sys模块
    Python——re模块
    Python——递归、二分查找算法
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5096284.html
Copyright © 2011-2022 走看看