2021-03-07 22:47发布
Spring开发团队意识到RMI服务和基于HTTP的服务(例如Hessian和Burlap)之间的空白。一方面,RMI使用Java标准的对象序列化机制,但是很难穿透防火墙。另一方面,Hessian和Burlap能很好的穿透防火墙,但是使用私有的对象序列化机制。
就这样,Spring的Http invoker应运而生了。HttpInvoker是一个新的远程调用模型,作为Spring框架的一部分,能够执行基于Http的远程调用,并使用Java的序列化机制。使用基于HttpInvoker的服务和使用基于Hessian/Burlap的服务非常相似。
二:spring如何配置HttpInvoker
项目整体结构,和上一篇一样
第一步:配置实体类,接口和接口实现类,和上一篇文章一模一样,这里就不再赘述了,请参考上一篇文章
第二步:配置HttpInvokerServiceExpoter,将接口实现类JinNangServiceImpl导出为服务
package chapter15.spittr.config;
import java.util.Properties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import chapter15.spittr.service.JinNangServiceImpl;
import chapter15.spittr.service.interfaces.JinNangService;
@Configuration
@ComponentScan
public class RootConfig {
@Bean
public JinNangService jinNangService(){
return new JinNangServiceImpl();
}
public HandlerMapping httpInvokerMapping(){
SimpleUrlHandlerMapping mapping=new SimpleUrlHandlerMapping();
Properties mappings=new Properties();
mappings.setProperty("/jinNang.service", "httpInvokerExporterJinNangService");
mapping.setMappings(mappings);
return mapping;
public HttpInvokerServiceExporter httpInvokerExporterJinNangService(JinNangService jinNangService){
HttpInvokerServiceExporter rmiServiceExporter=new HttpInvokerServiceExporter();
rmiServiceExporter.setService(jinNangService);
rmiServiceExporter.setServiceInterface(JinNangService.class);
return rmiServiceExporter;
是否有点似曾相识,我们很难找出这个bean的定义和上一篇所声明的bean有什么不同,唯一的区别就是类名:HttpInvokerServiceExporter。否则的话,这个导出器和其他远程服务的导出器没有任何区别。
如下图所示,HttpInvokerServiceExporter的工作方式是:HttpInvokerServiceExporter也是一个Spring的MVC控制器,他通过DispatcherServlet接受来自客户端的请求,并将这些请求转换成实现服务的Pojo的方法调用。
最多设置5个标签!
Spring开发团队意识到RMI服务和基于HTTP的服务(例如Hessian和Burlap)之间的空白。一方面,RMI使用Java标准的对象序列化机制,但是很难穿透防火墙。另一方面,Hessian和Burlap能很好的穿透防火墙,但是使用私有的对象序列化机制。
就这样,Spring的Http invoker应运而生了。HttpInvoker是一个新的远程调用模型,作为Spring框架的一部分,能够执行基于Http的远程调用,并使用Java的序列化机制。使用基于HttpInvoker的服务和使用基于Hessian/Burlap的服务非常相似。
二:spring如何配置HttpInvoker
项目整体结构,和上一篇一样
第一步:配置实体类,接口和接口实现类,和上一篇文章一模一样,这里就不再赘述了,请参考上一篇文章
第二步:配置HttpInvokerServiceExpoter,将接口实现类JinNangServiceImpl导出为服务
package chapter15.spittr.config;
import java.util.Properties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import chapter15.spittr.service.JinNangServiceImpl;
import chapter15.spittr.service.interfaces.JinNangService;
@Configuration
@ComponentScan
public class RootConfig {
@Bean
public JinNangService jinNangService(){
return new JinNangServiceImpl();
}
@Bean
public HandlerMapping httpInvokerMapping(){
SimpleUrlHandlerMapping mapping=new SimpleUrlHandlerMapping();
Properties mappings=new Properties();
mappings.setProperty("/jinNang.service", "httpInvokerExporterJinNangService");
mapping.setMappings(mappings);
return mapping;
}
@Bean
public HttpInvokerServiceExporter httpInvokerExporterJinNangService(JinNangService jinNangService){
HttpInvokerServiceExporter rmiServiceExporter=new HttpInvokerServiceExporter();
rmiServiceExporter.setService(jinNangService);
rmiServiceExporter.setServiceInterface(JinNangService.class);
return rmiServiceExporter;
}
}
是否有点似曾相识,我们很难找出这个bean的定义和上一篇所声明的bean有什么不同,唯一的区别就是类名:HttpInvokerServiceExporter。否则的话,这个导出器和其他远程服务的导出器没有任何区别。
如下图所示,HttpInvokerServiceExporter的工作方式是:HttpInvokerServiceExporter也是一个Spring的MVC控制器,他通过DispatcherServlet接受来自客户端的请求,并将这些请求转换成实现服务的Pojo的方法调用。
一周热门 更多>