使用Spring.Net将业务类封装成WebService
2010-05-08 00:14
351 查看
本文PDF下载
转载请注明出处
然而,编写的WebService其实已经在业务逻辑层中实现了,只不过又重复编写了一次。Spring.NET框架提供了一整套的解决方案来简化这一过程,开发人员只需要创建配置文件,即可以实现将现有的业务逻辑开发为WebService。
l ASP.NET
l Framework 3.5
l Spring.NET 1.2
在下面的示例中,简单地创建了一个无参数方法GetName,这个方法返回一个字符串:
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
在此配置中由“<resource uri="~/WebService.config"/>”指明了Spring配置文件的路径。
在红色注释的代码行中,如果用户访问了.asmx结尾的文件(ASP.NET的WebService资源),都将由Spring进行处理。
其代码如下:
Spring配置文件的格式及原理就不在这里讲述了,网络中有大量的资源。
输入URL:http://localhost:2287/SrpingWebServices/TestService.asmx
需要说明的是,TestService.asmx实际是不存在的,而是在WebService.config配置文件中,WebService对象的id。在本实例中就是:
这时候可以看到WebService的说明页面,如图所示:
点击“GetName”超链接,可以看到如下页面:
点击“调用”按钮,可以获得WebService执行结果:
至此,大功告成。
转载请注明出处
文章概述
在三层开发中,业务逻辑层不仅会在本系统中使用,而且常常会开放为WebService以供其他系统调用,从而共享计算能力。然而,编写的WebService其实已经在业务逻辑层中实现了,只不过又重复编写了一次。Spring.NET框架提供了一整套的解决方案来简化这一过程,开发人员只需要创建配置文件,即可以实现将现有的业务逻辑开发为WebService。
项目环境
l VS2008l 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> |
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> |
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> |
测试页面
启动测试服务器,我的端口是2287。输入URL:http://localhost:2287/SrpingWebServices/TestService.asmx
需要说明的是,TestService.asmx实际是不存在的,而是在WebService.config配置文件中,WebService对象的id。在本实例中就是:
<object id="TestService" type="Spring.Web.Services.WebServiceExporter, Spring.Web"> |
点击“GetName”超链接,可以看到如下页面:
点击“调用”按钮,可以获得WebService执行结果:
至此,大功告成。
本文资源
Spring.NET:官方下载相关文章推荐
- 使用Spring.net中对Ado.net的抽象封装来访问数据库
- 使用spring.net发布webservice时函数重名的问题的解决办法
- 使用Spring.Net构建WebService
- 使用spring.net发布webservice时函数重名的问题的解决办法
- Spring对JDBC封装时使用的一种CallBack来参数化一个操作的模式
- 使用spring集成xfire开发webservice接口时的namespace和参数名称等配置
- java使用AXIS2调用asp.net的WebService
- 在ASP.NET中使用SOAP Extensions捕获WebService异常
- 使用CXF与Spring集成实现RESTFul WebService
- 在Spring.Net中对于NHibernate.Caches.Prevalence的使用
- 使用Spring+CXF开发WebService
- webservice学习之使用Apache CXF和spring搭建webservice
- 使用注解和spring的JdbcTemplate进行封装实现类似Hibernate的功能
- SPRING.NET 1.3.2 学习21--使用对象或类的属性值进行注入
- spring.net 发布 webservice的一点心得
- ASP.NET WebService中使用ASP.NET_SessionId的问题说明
- spring boot 使用@ConfigurationPropertiesu将配置自动封装成实体类
- 使用CXF+Spring发布WebService,启动报错
- 基于wince.net的环境,使用pocketBuilder调用webservice所需安装环境和步骤
- ASP.Net Ajax应用初步:使用AJAX调用WebService