`

XFire与Spring结合的两种方式

阅读更多

1、使用org.codehaus.xfire.spring.XFireSpringServlet与ServiceBean

1.1 web.xml的配置

 <web-app>
 <display-name>Spring Image Database</display-name>
 <description>Spring Image Database sample application</description>
 <!--
  These values are used by ContextLoaderListener, defined immediately below.
        The files listed below are used to initialize the business logic portion of the application.
        Each dispatcher servlet (defined further down) has their own configuration file,
        which may or may not depend on items in these files.
    -->
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>
     classpath:applicationContext-webservice.xml
    </param-value>
    </context-param>
 <!-- Log4j configuration listener-->
 <listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
 <!-- Spring framework -->
 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

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

 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 
</web-app>

1.2 applicationContext-webservice.xml的配置:

<beans>

    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
   
    <bean name="echoService" class="org.codehaus.xfire.spring.ServiceBean">
        <property name="serviceBean" ref="echo"/>
        <property name="serviceClass" value="org.codehaus.xfire.test.Echo"/>
        <property name="inHandlers">
            <list>
                <ref bean="addressingHandler"/>
            </list>
        </property>
    </bean>

    <bean id="echo" class="org.codehaus.xfire.test.EchoImpl"/>

    <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>
 
   <bean name="bookService" class="org.codehaus.xfire.spring.ServiceBean">
        <property name="serviceBean" ref="bookServiceBean"/>
        <property name="serviceClass" value="org.codehaus.xfire.demo.BookService"/>
    </bean>

    <bean id="bookServiceBean" class="org.codehaus.xfire.demo.BookServiceImpl"/>

</beans>

1.3 这样将会发布两个service,BookService和EchoService。随后就可以使用client端进行测试了。

     //测试BookService
    public static void main(String args[])
    { 
        String serviceURL = "
http://127.0.0.1:9001/xfire/services/BookService";
        Service serviceModel = new ObjectServiceFactory().create(BookService.class,null,"
http://xfire.codehaus.org/BookService",null);
        XFireProxyFactory serviceFactory = new XFireProxyFactory();
        try
        {
            BookService service = (BookService) serviceFactory.create(serviceModel, serviceURL);
            Client client = Client.getInstance(service);
            client.addOutHandler(new OutHeaderHandler());
            Book[] books = service.getBooks();
            System.out.println("BOOKS:");
            for (int i = 0; i < books.length; i++)
            {
                System.out.println(books[i].getTitle());
            }
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
    }

1.4 忘了BookService及其实现了。

     public interface BookService
    {
              public Book[] getBooks();
   
              public Book findBook(String isbn);
   
             public Map getBooksMap();
   }

 

    public class BookServiceImpl implements BookService
    {
    private Book onlyBook;
   
    public BookServiceImpl()
    {
        onlyBook = new Book();
        onlyBook.setAuthor("Dan Diephouse");
        onlyBook.setTitle("Using XFire");
        onlyBook.setIsbn("0123456789");
     }

     public Book[] getBooks()
     {
        return new Book[] { onlyBook };
     }
   
     public Book findBook(String isbn)
     {
        if (isbn.equals(onlyBook.getIsbn()))
            return onlyBook;
       
        return null;
     }

     public Map getBooksMap() {
  Map result = new HashMap();
  result.put(onlyBook.getIsbn(), onlyBook);
  return result;
     }
    }

1.5 简单的测试就是通过IE,输入http://ip:port/context/services/BookService?wsdl或者http://ip:port/context/services/EchoService?wsdl,将会出现相应的wsdl文档。

     如果只是输入http://ip:port/context/services/BookService,会出现Invalid SOAP request.这也说明配置正确。

2、直接集成Spring(通过Spring的org.springframework.web.servlet.DispatcherServlet)

2.1 web.xml配置
<web-app>
<!-- START SNIPPET: xfire -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
    </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>

    <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>
<!-- END SNIPPET: xfire -->
</web-app>
2.2 xfire-servlet.xml配置
<beans>
    <!-- START SNIPPET: xfire -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/EchoService">
                    <ref bean="echo"/>
                </entry>
            </map>
        </property>
    </bean>

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

    <!-- Declare a parent bean with all properties common to both services -->
    <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>
        <property name="serviceBean">
            <ref bean="echoBean"/>
        </property>
        <property name="serviceClass">
            <value>org.codehaus.xfire.spring.example.Echo</value>
        </property>
    </bean>
    <!-- END SNIPPET: xfire -->
</beans>
2.3 余下的配置跟第一种方法一样。

3、另外xfire的官方文档上还有一种方法,是通过XBean与Spring结合来实现webservice的expose。还是觉得上面的两种方法比较好。既然已经与spring集成在一起了,何必再引入其他的呢?以后的维护是不是也要有问题呢?

分享到:
评论
3 楼 641216927 2009-08-21  
这种也行。
<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>
</bean>

<bean id="childrenSerivces" parent="echo">
<property name="serviceBean">
            <ref bean="echoBean"/>
        </property>
        <property name="serviceClass">
            <value>org.codehaus.xfire.spring.example.Echo</value>
        </property>
</bean>
2 楼 yangsheng 2009-01-12  
我就调不通,客户端测式一直不成功
1 楼 wuyingsong 2008-11-24  
jdk必须和tomcat(如果用的话)匹配,jdk1.5不能和tomcat5.0.28,只能用tomcat5.5,好象写这个的人都没说明,结果有的人能跑通,有的人却不行,呵呵

相关推荐

    xfire+spring开发webservice

    结合XFire和Spring,有以下两种主要的方式来开发Web服务: 1. **基于注解的方式**:Spring支持使用JSR-181注解来定义Web服务接口和服务实现。通过在接口和实现类上添加相应的注解,如`@WebService`、`@SOAPBinding`...

    xfire+spring+安全认证

    总的来说,"xfire+spring+安全认证"是一个关于利用XFire作为Web服务实现工具,结合Spring框架的安全模块,来构建安全的分布式应用程序的主题。它涵盖了Web服务开发、认证与授权、以及安全通信等多个IT领域的关键知识...

    webservice(xfire)+spring的二种实现方式

    在这个主题中,我们将深入探讨如何使用XFire与Spring框架结合,实现Web服务的两种不同方式。 首先,XFire是一个已不再维护的开源项目,它被Apache CXF所取代。尽管如此,理解XFire对于理解CXF或其他Web服务实现仍有...

    webservice xfire spring2.0完整实例

    - 客户端有两种方式与Web服务交互: 1. **通过接口**:利用`LocalXFireWebServiceProxyFactoryBean`创建接口的代理,调用接口方法即可调用Web服务。 2. **通过URL**:可以使用`WebClient`类直接构造请求并发送,...

    xfire+spring+webservice

    将XFire与Spring结合,可以充分发挥各自的优势。首先,Spring的IoC容器可以管理XFire的服务实例,使得服务的生命周期控制更加简洁。其次,通过Spring的AOP,我们可以方便地添加事务管理、日志记录等通用功能到Web...

    xfire+spring webservice

    将XFire与Spring结合,可以充分利用Spring的IoC(Inversion of Control)和AOP(Aspect Oriented Programming)特性,使Web服务的开发更为灵活和可维护。以下是如何在Spring中集成XFire的基本步骤: 1. **添加依赖*...

    spring与xfire结合

    标题中的“spring与xfire结合”指的是在Java开发中,Spring框架与XFire服务框架的集成应用。Spring是一个广泛使用的开源框架,它提供了一个全面的编程和配置模型,旨在简化企业级Java应用的开发。而XFire则是一个轻...

    使用XFire+Spring构建Web Service

    【使用XFire+Spring构建Web Service】是一种...总的来说,XFire结合Spring为Web Service开发提供了一种高效且易于维护的解决方案,它的高性能、丰富的功能和良好的社区支持使得它在Web Service开发领域具有很高的价值。

    java webservice之xfire与spring2集成(三)

    集成XFire与Spring2提供了一种灵活、可扩展的方式来构建和管理Web服务。这种集成方法使得开发者能够充分利用Spring的容器管理和XFire的Web服务特性,提高开发效率,同时降低了维护复杂性。通过深入理解这两个框架的...

    xFire+spring webService 示例

    在这个"xFire+Spring WebService示例"中,我们将深入探讨这两个技术的结合以及如何通过它们实现Web服务。 首先,让我们了解xFire。xFire(现为Apache CXF)是一个用于构建和部署Web服务的Java库。它支持多种Web服务...

    Spring + Xfire + 注解方式

    描述中提到的博客链接虽然无法直接访问,但根据常规的博客内容,博主可能详细解释了如何将Spring与Xfire结合,以及如何通过注解的方式来替代传统的XML配置,以实现更加简洁、直观的代码结构。 在使用注解的方式时,...

    xfirespring整合使用原代码

    本篇文章将深入探讨"xfirespring整合使用HELLOworld原代码"的相关知识点。 首先,我们需要理解XFire的基本概念。XFire是Apache CXF项目的一部分,它提供了一种快速、简单的构建和部署SOAP Web服务的方式。XFire通过...

    Xfire Spring Hibernate 发布WebService

    【Xfire Spring Hibernate 发布WebService】是将Xfire、Spring和Hibernate这三种技术结合,用于在MyEclipse环境中创建和发布Web服务。Xfire是一个基于Java的SOAP和REST Web服务框架,Spring则提供了依赖注入和AOP...

    Spring+xFire实现webService

    Spring+xFire 实现 WebService 是一种在 Java 开发中创建和使用 Web 服务的方式,它结合了 Spring 框架的灵活性和 xFire(现在称为 Apache CXF)的 Web 服务功能。以下是对这个技术栈的详细说明: 1. **环境配置**...

    Spring+hibernate+Xfire

    Xfire支持SOAP和RESTful两种Web服务风格,使得服务提供者和消费者之间的通信更为灵活。Xfire与Spring的集成,可以让开发者轻松地在Spring应用中发布和消费Web服务。 描述中提到的"Xfire和Hibernate结合实例,包括和...

    Spring+xfire实现WebService

    在IT行业中,构建Web服务是常见的任务,而Spring框架与XFire的结合提供了一种高效且灵活的方式来创建和消费Web服务。本篇文章将深入探讨如何使用Spring和XFire来实现Web服务,以及这两个组件的核心功能和它们之间的...

    xfire-spring:xfire1.2.6+spring3.2.5webservice示例程序

    xfire-spring项目是将XFire(一款基于Java的Web服务实现)与Spring框架结合,用于简化Web服务的开发、部署和测试。本文将深入探讨xfire-spring项目,特别是基于xfire1.2.6和spring3.2.5版本的Web服务示例程序,帮助...

    spring+axis2_xfire整合的webservice

    【Spring + Axis2 + XFire 整合的Web Service】是一种在Java环境下构建Web服务的解决方案,它结合了Spring框架的灵活性和Axis2、XFire的Web服务处理能力。Web服务是一个基于开放标准的分布式计算模型,允许不同系统...

Global site tag (gtag.js) - Google Analytics