Spring是目前最流行的JavaEE Framework,但是使用Spring的Spring-WS开发WebService却十分繁琐。XFire是一个简化WebService开发的开源项目,通过Spring和XFire的结合可以大大简化基于Spring Framework的应用中的WebService开发。下面介绍的是一种比较简单实用的开发方式。
XFire + Spring构建Web Services服务器端我知道的有两种办法,这是第一种
开发环境是:myeclipse8.6,Tomact6.0
Spring版本是2.5 XFire版本是1.2.6
新建web service project项目WS_XFire_Service,下载xfire1.2.6,下载后解压,将xfire-all-1.2.6.jar添加到项目中,lib下面是其他需要的jar包,不过此处注意:lib中包含一个spring-1.2.6.jar,这个jar包会和接下来你所添加的spring.jar冲突,可以删除spring-1.2.6.jar,也可不添加接下来的spring.jar
spring需要的jar包如下:
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>
<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>
<?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>
接口
package com.kuaff.xfire.samples; public interface MathService { public int add(int p1, int p2); }
package com.kuaff.xfire.samples.impl; import com.kuaff.xfire.samples.MathService; public class MathServiceImpl implements MathService { //此处变量str与程序逻辑无关,只是为了验证spring是否成功注入 private String str; public int add(int p1, int p2) { System.out.println(str); return p1 + p2; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } }
将上面实现的服务加入到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"> <!-- 此处mathWS仅被下文引用--> <bean id="mathWS" class="com.kuaff.xfire.samples.impl.MathServiceImpl"> <property name="str" value="123456789" /> </bean> <!-- 此处MathService为被客户端使用的服务名--> <bean name="MathService" class="org.codehaus.xfire.spring.ServiceBean"> <property name="serviceBean" ref="mathWS"/> <property name="serviceClass" value="com.kuaff.xfire.samples.MathService"/> <property name="inHandlers"> <list> <ref bean="addressingHandler"/> </list> </property> </bean> <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/> </beans>
部署项目,在浏览器输入http://localhost:8080/WS_XFire_Service/services/MathService?wsdl验证部署是否成功
原文章:http://www.blogjava.net/chaocai/archive/2007/09/17/145932.html