已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Spring HTTP invoker简介Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,这有点类似于webservice,但又不同于webservice,区别如下:webserviceHTTP invoker跨平台,跨语言只支持java语言支持SOAP,提供wsdl不支持结构庞大,依赖特定的webservice实现,如xfire等结构简单,只依赖于spring框架本身项目中使用哪种远程调用机制取决于项目本身的要求。HTTP invoker服务模式说明:1.服务器端:通过HTTP invoker服务将服务接口的某个实现类提供为远程服务2.客户端:通过HTTP invoker代理向服务器端发送请求,远程调用服务接口的方法3.服务器端与客户端通信的数据需要序列化配置服务器端和客户端的步骤配置服务器端1.添加springJAR文件建议使用spring2+.jar版本2.创建服务接口3.创建服务接口的具体实现类4.公开服务配置客户端1.添加springJAR文件建议使用spring2+.jar版本2.创建服务接口3.访问服务实例讲解服务器端1.服务接口:UcService.java它提供两项服务,查询用户信息和记录日志,如下:publicinterfaceUcService publicUserInfo getUserInfobyName(String userName);publicintrecordLog(String username, String point, String operate, String desc);说明:举这个列子是因为其比较有代表性,它将展示普通数据类型(int,long等)和复杂数据类型(DTO等)的远程调用方式。UserInfo是一个普通的DTO,代码如下:publicclassUserInfoimplementsSerializable privatestaticfinallongserialVersionUID= -6970967506712260305L;/*用户名*/privateStringuserName;/*电子邮箱*/privateStringemail;/*注册日期*/privateDateregistDate;publicString getUserName() returnuserName;publicvoidsetUserName(String userName) this.userName= userName;publicString getEmail() returnemail;publicvoidsetEmail(String email) this.email= email;publicDate getRegistDate() returnregistDate;publicvoidsetRegistDate(Date registDate) this.registDate= registDate;注意:因为是在网络间传输对象,所以需要将UserInfo实现Serializable接口,并指定一个serialVersionUID(任意值即可,同时客户端也要有这个类,否则在客户端接收对象时会因为serialVersionUID不匹配而出现异常)回到UcService.java,它提供了两个服务(在这里一个方法代表一个服务功能),我们需要具体的实现类来实现真正的服务2.实现类是UCServiceImpl.javapublicclassUCServiceImplimplementsUcService privatestaticLoggerpointrecordlog= Logger.getLogger(pointrecordlog);privatestaticLoggerlogger= Logger.getLogger(UCServiceImpl.class);privateUcFacadeucFacade;publicvoidsetUcFacade(UcFacade ucFacade) this.ucFacade= ucFacade;publicUserInfo getUserInfobyName(String userName) UserInfo user =null;tryuser =ucFacade.getUserInfoDetail(userName);logger.debug(get userinfo success byusername:+ userName);catch(Throwable t) logger.error(get userinfo fail byusername:+ userName, t);returnuser;publicintrecordLog(String username, String point, String operate, String desc) intresult = 0;(username + - + point + - + operate + - + desc);catch(Throwable t) result = -1;logger.error(t);returnresult;说明:ucFacade是通过spring注入的一个数据查询类,因为它与http invoker没有直接关系,所以不进行介绍。3.公开服务UcService.javaWEB-INF/application-context.xml:将接口声明为HTTP invoker服务说明:HttpInvokerServiceExporter实际上是一个spring mvc控制器,它处理客户端的请求并调用服务实现。WEB-INF/service-servlet.xml:HttpInvokerServiceExporter实际上是一个spring mvc控制器,所以需要为其提供spring URL处理器,这里我们使用SimpleUrlHandlerMappinghttpServiceWEB-INF/web.xml:配置spring监听及DispatcherServletcontextConfigLocation/WEB-INF/application-context.xmlorg.springframework.web.context.ContextLoaderListenerserviceorg.springframework.web.servlet.DispatcherServlet1service/service/*说明:不了解为什么这么配置的可以去看看spring mvc方面的资料。好了,经过以上配置,一个基于spring HTTP invoker的远程服务就完成了,服务的地址为:http:/$serviceName:$port/$contextPath/service/httpService客户端1.创建服务接口及网络间传输的DTO类为了方便,可以将服务器端创建好的的UcService.java和UserInfo.java拷贝到客户端,或打个jar包放到lib下。2.配置访问服务WEB-INF/application-context.xml:如果项目中已经存在spring配置文件,则不需要创建该文件,需要配置HTTP invoker的代理http:/$serviceName:$port/$contextPath/service/httpService说明:客户端使用HttpInvokerProxyFactoryBean代理客户端向服务器端发送请求,请求接口为UcService的服务注意:需要修改serviceUrl为实际的服务器地址WEB-INF/web.xml:配置spring监听如果项目没有spring环境,则需要在web.xml中加入对spring的支持contextConfigLocation/WEB-INF/application-context.xmlorg.springframework.web.context.ContextLoaderListener3.访问服务方法u读取spring上下文,以远程调用getUserInfobyName方法为例在jsp,servlet,action等等文件中UcServiceservice= (UcService) WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext().getBean(httpService);UserInfouser=service.getUserInfobyName(hanqunfeng);如果不想配置spring运行环境,可以使用如下方式:ApplicationContext applicationContext=newFileSystemXmlApplicationContext(classpath:application-context.xml);service = (UcService) applicationContext.getBean(httpService);u依赖注入,远程调用recordLog方法为例在WEB-INF/application-context.xml中加入如下配置:为qin.test.abc中加入对service的set方法:privateUcServiceservice;publicvoidsetService(UcService service)this.service= service;publicString recordUserLog(String username,String point,String operate,String desc)String result =service.recordLog(username, point, operate, desc);returnresult;关于服务器端配置的补充说明:有一个误区:有些关于springMVC的书 上说,如果没有明确声明一个处理适配器,默认会使用 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,这个适配器 专门负责处理所有实现了org.springframework.web.servlet.mvc.Controller 接口的处理器,我就是受其影响,认为 org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter实现的是 org.springframework.web.HttpRequestHandler接口,所以按理说应该使用的处理适配器是 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,但实际上并不会出现异 常。其实,原因是因为spring默认会使用四个处理适配器(参看DispatcherSperties,spring2.5,spring2.0只默认三个,2.5增加注解方式):org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,/org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,/org.springframework.web.servlet.mvc.throwaway.ThrowawayControllerHandlerAdapter,/org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter关于DispatcherSperties的详细信息可以参看:/hanqunfeng/archive/2010/01/08/5161319.aspx但是,如果明确声明了其它的处理适配器,比如 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter, 等等,则默认规则则会覆盖,需要明确声明 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter这个处理适配器,否则系 统会抛异常:javax.servlet.ServletException: No adapter for handler org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter179bd14: Does your handler implement a supported interface like Controller?所以,建议在使用sp
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 送电线路停电更换绝缘子危险点分析及预控措施培训
- 二硫化碳危险特性及安全注意事项培训
- 2026二上数学角的初步认识无生上课课件
- 2026中国涡流泵行业周期性特征与逆周期调节策略研究
- 2026中国污水处理设备行业供需现状及投资风险评估报告
- 2026中国玻璃纤维土工格栅基础设施建设需求与行业标准完善报告
- 2026中国新能源电池行业市场现状供需分析及企业投资评估规划分析研究报告
- 2026中国电子竞技赛事IP运营与粉丝经济战略研究报告
- 英语爱国试题及详细答案
- 2025-2026学年广东省湛江市高一(下)期末物理试卷(含答案)
- 国企合规管理课件
- 高二课件语文(统编版选择性必修上册)52大学之道
- 高等职业教育分类招生考试现存问题和优化策略研究
- 2025年下半年太仓市城市建设投资集团限公司公开招聘3人易考易错模拟试题(共500题)试卷后附参考答案
- 热风枪拆焊台的使用方法培训
- 《新能源技术》课件-4风力发电技术
- 2025既有建筑消防改造设计指南
- 肝受损的护理诊断及措施
- 陕西水务发展集团招聘笔试冲刺题2025
- 《转化医学概述》课件
- AI在电商行业中的应用
评论
0/150
提交评论