Hessian和Burlap都是基于HTTP的,他们都解决了RMI所头疼的防火墙渗透问题。但当传递过来的RPC消息中包含序列化对象时,RMI就完胜Hessian和Burlap了。
因为Hessian和Burlap都是采用了私有的序列化机制,而RMI使用的是Java本身的序列化机制。如果数据模型非常复杂,那么Hessian/Burlap的序列化模型可能就无法胜任了。
Spring开发团队意识到RMI服务和基于HTTP的服务之前的空白,Spring的HttpInvoker应运而生。
Spring的HttpInvoker,它基于HTTP之上提供RPC,同时又使用了Java的对象序列化机制。

程序的具体实现
一、首先我们创建一个实体类,并实现Serializable接口

package entity;
import java.io.Serializable;
public class Fruit implements Serializable {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}

二、创建一个接口

package service;
import java.util.List;
import entity.Fruit;
public interface FruitService {
List<Fruit> getFruitList();
}

三、创建一个类,并实现步骤二中的接口

package service.impl;
import java.util.ArrayList;
import java.util.List;
import service.FruitService;
import entity.Fruit;
public class FruitServiceImpl implements FruitService {
public List<Fruit> getFruitList() {
List<Fruit> list = new ArrayList<Fruit>();
Fruit f1 = new Fruit();
f1.setName("橙子");
f1.setColor("黄色");
Fruit f2 = new Fruit();
f2.setName("苹果");
f2.setColor("红色");
list.add(f1);
list.add(f2);
return list;
}
}

四、在WEB-INF下的web.xml中配置SpringMVC需要的信息

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

五、在applicationContext.xml配置需要导出服务的bean信息

<bean id="furitService" class="service.impl.FruitServiceImpl"></bean>
<bean id="FuritService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
p:serviceInterface="service.FruitService" p:service-ref="furitService" />

六、在WEB-INF下创建springMvc-servlet.xml文件,并配置urlMapping

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/fruitService">FuritService</prop>
</props>
</property>
</bean>
</beans>

七、在applicationContext.xml编写客户端所需要获得服务的bean信息

<bean id="getFruitService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
p:serviceInterface="service.FruitService"
p:serviceUrl="http://localhost:8080/SpringHttpInvoker/fruitService" />

八、编写测试代码

package test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.Fruit;
import service.FruitService;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
FruitService fruitService = (FruitService) ctx
.getBean("getFruitService");
List<Fruit> fruitList = fruitService.getFruitList();
for (Fruit fruit : fruitList) {
System.out.println(fruit.getColor() + "的" + fruit.getName());
}
}
}

将项目部署到Tomcat上,启动Tomcat服务,并运行测试代码
===========控制台========

黄色的橙子
红色的苹果

=======================

Java学习之路-Spring的HttpInvoker学习的更多相关文章

  1. [Java开发之路](16)学习log4j日志

    1. 新建一个Javaproject.导入Jar包(log4j-1.2.17.jar) Jar包下载地址:点击打开链接 2. 配置文件:创建并设置log4j.properties # 设置 log4j ...

  2. mybatis源码学习(二)--mybatis+spring源码学习

    这篇笔记主要来就,mybatis是如何利用spring的扩展点来实现和spring的整合 1.mybatis和spring整合之后,我们就不需要使用sqlSession.selectOne()这种方式 ...

  3. Android开发学习之路-二维码学习

    这个月装逼有点少了,为什么呢,因为去考软件射鸡师了,快到儿童节了,赶紧写篇博纪念一下逝去的青春,唔,请忽略这句话. 二维码其实有很多种,但是我们常见的微信使用的是一种叫做QRCode的二维码,像下面这 ...

  4. Spring学习笔记:Spring整合Mybatis学习PPT(三:整合思路)

    三.Spring-Mybatis的整合思路

  5. Android开发学习之路--Android系统架构初探

    环境搭建好了,最简单的app也运行过了,那么app到底是怎么运行在手机上的,手机又到底怎么能运行这些应用,一堆的电子元器件最后可以运行这么美妙的界面,在此还是需要好好研究研究.这里从芯片及硬件模块-& ...

  6. Python学习之路-Day2-Python基础3

    Python学习之路第三天 学习内容: 1.文件操作 2.字符转编码操作 3.函数介绍 4.递归 5.函数式编程 1.文件操作 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个 ...

  7. Spring源码学习

    Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...

  8. 微软企业库5.0 学习之路——第二步、使用VS2010+Data Access模块建立多数据库项目

    现在我就开始进入学习之路的第二步——Data Access模块,这个模块是企业库中被使用频率最高的模块,它很好的封装了数据库操作应用,为我们进行多数据库系统开发提供了便利,只需更改配置文件就 可以很快 ...

  9. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

随机推荐

  1. Matlab练习——矩阵和数组的操作

    题目来自:<战胜MATLAB必做练习50道> 题目有更改,改成了我想写的样子. 1. 创建一个3×3矩阵,并将其扩充为4×5矩阵 clear; clc; mat1 = ones(,) ma ...

  2. 【.netcore学习】.netcore添加到 supervisor 守护进程自启动报错

    配置 supervisor [program:HelloWebApp] command=dotnet run directory=/home/python/dotnet/myweb/mywebapi ...

  3. 【cs229-Lecture14】主成分分析法

    本节课内容: 因子分析 ---因子分析中的EM步骤的推导过程 主成份分析:有效地降低维度的方法 因子分析 混合高斯模型的问题 接下来讨论因子分析模型,在介绍因子分析模型之前,先看高斯分布的另一种写法, ...

  4. Qt编写输入法终极版V2018

    输入法是很多Qt+嵌入式linux开发的同学的痛,自从5.7自带了输入法后,这个痛终于缓解了不少,不过还有大量的嵌入式linux程序停留在qt4时代,为此特意选择了QWidget来写这个输入法,为了兼 ...

  5. Python OS 文件/目录方法

    Python OS 文件/目录方法 os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os. ...

  6. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十四:储存模块

    实验十四比起动手笔者更加注重原理,因为实验十四要讨论的东西,不是其它而是低级建模II之一的模块类,即储存模块.接触顺序语言之际,“储存”不禁让人联想到变量或者数组,结果它们好比数据的暂存空间. . i ...

  7. 【CF873F】Forbidden Indices 后缀自动机

    [CF873F]Forbidden Indices 题意:给你一个串s,其中一些位置是危险的.定义一个子串的出现次数为:它的所有出现位置中,不是危险位置的个数.求s的所有子串中,长度*出现次数的最大值 ...

  8. 美团开源 SQL 优化工具 SQLAdvisor

    https://www.oschina.net/news/82725/sqladvisor-opensource https://github.com/Meituan-Dianping/SQLAdvi ...

  9. python之traceback

    traceback 模块允许你在程序里打印异常的跟踪返回 (Traceback)信息 1.1 traceback.print_exc() File: traceback-example-1.py # ...

  10. C++和Java中的静态Static用法

    C++和Java中都有的一个静态关键字Static,可以放在类中的变量或者函数之前,就成了静态变量或者静态函数. 静态变量又分为静态全局变量和静态局部变量,可参见网上大神总结的C++全局变量,局部变量 ...