2007-09-17

XFire学习

看了下XFire,记录相关资料。

一.概述
官网http://xfire.codehaus.org/

XFire 是 codeHaus 组织提供的一个开源框架,它构建了 POJO 和 SOA 之间的桥梁,主要特性就是支持将 POJO 通过非常简单的方式发布成 Web 服务,这种处理方式不仅充分发挥了 POJO 的作用,简化了 Java 应用转化为 Web 服务的步骤和过程,也直接降低了 SOA 的实现难度,为企业转向 SOA 架构提供了一种简单可行的方式。


二.技术架构

XFire框架是一种基于Servlet技术的SOA应用开发框架,要正常运行基于XFire应用框架开发的企业应用,除了XFire框架本身之外,还需要JDK和Servlet容器的支持(XFire 支持在多种 Servlet 容器中运行,包括 Websphere、Weblogic、TOMCAT 等)

三.XFire的使用
详细讲解http://www.ibm.com/developerworks/cn/java/j-lo-xfire/#h1

四.Spring集成XFire
XFire可以很好的集成到Spring中,Spring的代码已经做了这方面的集成。
将 POJO 发布成 Web 服务的基本步骤如下
1.创建 Web 服务接口
声明和实现该 Web 服务对外暴露的接口;
接口Echo.java:
[code]Echo.java package org.codehaus.xfire.spring.example; public interface Echo{ String echo(String in); } 实现EchoImpl.java package org.codehaus.xfire.spring.example; public class EchoImpl implements Echo{ public String echo(String in){ return in; } }[/code]

META-INF/xfire/service.xml文件可以省略了,因为web服务的定义在xfire-servlet.xml中可以找到。

2.配置普通bean
下面要做的工作就是配置了,在WEB-INF文件夹下创建applicationContext.xml文件,这是Spring的配置文件,如果你使用其他的Spring配置文件,可以将下面的bean添加到那个配置文件中:
[code]<?xml version=1.0 encoding=UTF-8?> <beans> <bean id=echoBean class=org.codehaus.xfire.spring.example.EchoImpl/> </beans>[/code]
定义了echoBean,这个Bean就是我们的实现类,当然你也可以在这个文件中定义其他的需要Spring管理的bean。

3.创建xfire-servlet.xml文件
在WEB-INF文件夹下创建xfire-servlet.xml文件,根据Spring规范,这个文件名起做xfire-servlet.xml,其中xfire是web.xml配置的DispatcherServlet的名称,这个文件的配置是关键:
xfire-servlet.xml
[code]<?xml version=1.0 encoding=UTF-8?> <beans> <bean class=org.springframework.web.servlet.handler.SimpleUrlHandlerMapping> <property name=urlMap> <map> <!--这个文件的上半部分将EchoService这个URL和echo这个bean联系在一起--> <entry key=/EchoService><ref bean=echo></entry> </map> </property> </bean> <!--echo定义了Web服务的bean和服务接口,其中echoBean是我们在applicationContext.xml中配置的那个Bean--> <bean id=echo class=org.codehaus.xfire.spring.remoting.XFireExporter> <!--固定内容--> <property name=serviceFactory><ref bean=xfire.serviceFactory</property> <property name=xfire><ref bean=xfire</property> <!--serviceBean是实现的类--> <property name=serviceBean><ref bean=echoBean</property> <!--serviceClass是接口--> <property name=serviceClass> <value>org.codehaus.xfire.spring.example.Echo</value> </property> </bean> </beans>[/code]

4.修改web.xml
[code]<?xml version="1.0" encoding="ISO-8859-1"?> <!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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml classpath:org/codehaus/xfire/spring/xfire.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>/*</url-pattern> </servlet-mapping> ……… </web-app> [/code]

需要注意这个文件的三个部分:
1.在定义contextConfigLocation参数时一定要加上[code]classpath:org/codehaus/xfire/spring/xfire.xml[/url]

2.定义listener:org.springframework.web.context.ContextLoaderListener

3.定义DispatcherServlet:xfire

调用这个Web服务 http://localhost:8080/xfire/EchoService
查看wsdl文档 http://localhost:8080/xfire/EchoService?wsdl

以上即为一个在spring中集成XFire的例子。
评论
figeonline 2007-11-27
值得參考。
发表评论

您还没有登录,请登录后发表评论