Spring整合XFire開發WebService

來源:互聯網
上載者:User
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

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<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="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<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> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.