文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>利用Spring解耦VS的WebService

利用Spring解耦VS的WebService

时间:2010-10-22  来源:linFen

 
      WebService项目利用[WebService][WebMethod]等Attribute来描述Web服务和它所提供的方法。从某种角度来说,这样做“污染”了原本干净的代码。下面附上原来的代码:

    public class HelloWorldService
    {
        public string HelloWorld()
        {
            return "Hello World";
        }
    }


       但是如果不用Attribute来描述WebService,我们应该如何去提供一个WebService,而且怎么做才能不“污染”里面的代码呢?Spring为这个问题提供了一个很好的解决方案。正因为有Spring,一个平平无奇的HelloWorldService类,几乎不用进行任何修改,就能够利用其提供一个WebService。首先我们定义一个接口,来为ServerSide和ClientSide有一个共同的语言。再让HelloWorldService实现这个接口。

    interface IHelloWorld
    {
        string HelloWorld();
    }
    public class HelloWorldService:IHelloWorld
    {
        public string HelloWorld()
        {
            return "Hello World";
        }
    }


      然后是做好ServerSide的Web.Config。

<configSections> 
   <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
   </sectionGroup>
</configSections>

<spring>
   <context>
     <resource uri="objects.xml"/>
   </context>
</spring>

<httpHandlers>
      <add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/>
</httpHandlers>

<httpModules>
      <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
</httpModules>


      配置好ServerSide的objects.xml,主要内容如下:

  <object id="IHelloWorld" type="SpringService.HelloWorldService, SpringService"/>

  <object id="HelloWorldService" type="Spring.Web.Services.WebServiceExporter, Spring.Web">
    <property name="TargetName" value="IHelloWorld"/>
    <property name="Description" value="Test!Test!Test!" />
    <property name="Namespace" value="http://myApp/webservices"/>
  </object>


      WebServiceExporter负责输出WebService。只要重新编译一次项目,服务器就会提供http://localhost:2955/HelloWorldService.asmx这样的WebService。简单吧?实际代码中只添加了一个接口,完全符合面向对象编程。下面介绍ClientSide如何通过接口调用HelloWorldService。

      ClientSide对于Spring的配置App.Config更简单,如下:

<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
    </sectionGroup>
  </configSections>
  <spring>
    <context>
      <resource uri="objects.xml"/>
    </context>
  </spring>
</configuration>


      接着是ClientSide的object.xml,

  <object id="HelloWorld" type="Spring.Web.Services.WebServiceProxyFactory, Spring.Services">
    <property name="ServiceUri" value="http://localhost:2955/HelloWorldService.asmx"/>
    <property name="ServiceInterface" value="Interfaces.IHelloWorld, Interfaces"/>
  </object>

 
     WebServiceProxyFactory恰如其名,负责产生WebService访问代理。下面只要把HelloWorld注入到你想调用WebService的类中,就行了。最简单的调用方法是:IHelloWorld ws = GetObject() As IHelloWorld 然后,ws.HelloWorld就可以了。

      总结:Spring为Service创建WebService时清除了不干净的代码,为ClientSide创建访问代理而不需要每添加一个WebService就添加一次Web引用。当两者需要更换新的WebService时,只需要修改配置文件,就能够达到相应的目的,而不需要改动代码。Spring能够把new的代码完全从代码中分离出来,降低了对象之间的耦合性。用了Spring后,由于代码都是基于接口而编写的,更利于做Mock测试(mock测试:就是在测试过程中,对于某些不容易构造或者 不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。mock对象:这个虚拟的对象就是mock对象。mock对象就是真实对象在调试期间的代替品。)

相关阅读 更多 +
排行榜 更多 +
西安交大通

西安交大通

生活实用 下载
长江云通

长江云通

生活实用 下载
translatez

translatez

生活实用 下载