您的位置:首页 > 编程语言 > Java开发

使用Spring.Net将业务类封装成WebService

2010-05-08 00:14 351 查看
本文PDF下载

转载请注明出处

文章概述

在三层开发中,业务逻辑层不仅会在本系统中使用,而且常常会开放为WebService以供其他系统调用,从而共享计算能力。

然而,编写的WebService其实已经在业务逻辑层中实现了,只不过又重复编写了一次。Spring.NET框架提供了一整套的解决方案来简化这一过程,开发人员只需要创建配置文件,即可以实现将现有的业务逻辑开发为WebService。

项目环境

l VS2008

l ASP.NET

l Framework 3.5

l Spring.NET 1.2

业务逻辑接口与实现类

在使用Spring.NET提供的将业务逻辑类开放为WebService方式时,此业务逻辑类必须要有一个接口

在下面的示例中,简单地创建了一个无参数方法GetName,这个方法返回一个字符串:

接口

namespace Test.IService.Default

{

public interface IUserService

{

string GetName();

}

}

实现类

namespace Test.Service.Default

{

public class UserService : IUserService

{

public string GetName()

{

return "Sunny D.D";

}

}

}

引入Spring.NET

需要引入的Spring.NET框架的dll文件列表如下:

1. antlr.runtime.dll

2. Common.Logging.dll

3. Spring.Aop.dll

4. Spring.Core.dll

5. Spring.Data.dll

6. Spring.Web.dll

7. Spring.Web.Extensions.dll

Web.config配置文件

configSections节点

<configSections>

<sectionGroup name="spring">

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

<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>

<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>

</sectionGroup>

</configSections>

Spring节点

<appSettings/>

<connectionStrings/>

<spring>

<parsers>

<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/>

<parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/>

<parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop"/>

</parsers>

<context>

<resource uri="~/WebService.config"/>

</context>

</spring>

在此配置中由“<resource uri="~/WebService.config"/>”指明了Spring配置文件的路径。

httpHandlers节点

<httpHandlers>

<remove verb="*" path="*.asmx"/>

<add path="*.asmx" verb="*" type="Spring.Web.Script.Services.ScriptHandlerFactory, Spring.Web.Extensions" validate="false" /> <!--这里是将业务逻辑类封装为WebService的关键配置-->

<add path="*.aspx" verb="*" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />

</httpHandlers>

在红色注释的代码行中,如果用户访问了.asmx结尾的文件(ASP.NET的WebService资源),都将由Spring进行处理。

httpModules节点

<httpModules>

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

</httpModules>

WebService.config配置文件

在网站根目录中创建WebService.config,如图所示:





其代码如下:

<?xml version="1.0" encoding="utf-8" ?>

<objects xmlns="http://www.springframework.net"

xmlns:db="http://www.springframework.net/database"

xmlns:tx="http://www.springframework.net/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<object id="Root.Test.UserService"

type="Test.Service.Default.UserService, Test.Service.Default"

scope="request">

</object>

<object id="TestService" type="Spring.Web.Services.WebServiceExporter, Spring.Web">

<property name="TargetName" value="Root.Test.UserService"/>

<property name="Namespace" value="http://tempuri.org/"/>

<property name="Description" value="测试服务"/>

<property name="MemberAttributes">

<dictionary>

<entry key="*">

<object type="System.Web.Services.WebMethodAttribute, System.Web.Services">

</object>

</entry>

</dictionary>

</property>

</object>

</objects>

Spring配置文件的格式及原理就不在这里讲述了,网络中有大量的资源。

测试页面

启动测试服务器,我的端口是2287。

输入URL:http://localhost:2287/SrpingWebServices/TestService.asmx

需要说明的是,TestService.asmx实际是不存在的,而是在WebService.config配置文件中,WebService对象的id。在本实例中就是:

<object id="TestService" type="Spring.Web.Services.WebServiceExporter, Spring.Web">

这时候可以看到WebService的说明页面,如图所示:





点击“GetName”超链接,可以看到如下页面:





点击“调用”按钮,可以获得WebService执行结果:





至此,大功告成。

本文资源

Spring.NET:官方下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: