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. RAC的搭建(三)--Grid的安装

    1. 安装cvuqdisk.rpm补丁包 在两个Oracle RAC 所有节点上安装操作系统程序包cvuqdisk.如果没有cvuqdisk,集群验证实用程序就无法发现共享磁盘. 该包在p104045 ...

  2. PowerShell的初步学习

    今天要重新学习一钟语法,由于工作中项目的需要,不得不说学习新的语言是必不可少的.          Windows PowerShell 是一种命令行外科程序和脚本环境,使命令行用户和脚本编写者可以利 ...

  3. MyBatis批量更新for Mysql 实例

    <update id="UpdatePwd" parameterType="java.util.List"> UPDATE FP_USER_BASE ...

  4. 基础知识《零》---一张图读懂JDK,JRE,JVM的区别与联系

  5. Jquery-无法有效获取当前窗口高度

    今天碰到个很奇怪的事情,那就是滚动条往下滚动时候没有触发提示,反而是往上滚动的时候,触发了提示.百思不得其解,尤其是拿了美工大大的切图过来,一点问题都没有. 那么就进行console.log输出查看了 ...

  6. Linux平台下mysql的ODBC配置方法

    在安装配置之前, 需要先大概了解一下MyODBC的架构. MyODBC体系结构建立在5个组件上,如下图所示: Driver Manager: 负责管理应用程序和驱动程序间的通信, 主要功能包括: 解析 ...

  7. elasticsearch基础----->elasticsearch环境的搭建

    这里面我们主要是在ubuntu系统上对elasticsearch进行一个环境的搭建,记录一下这个过程中遇到的一些问题以及解决方案.我总是躲在梦与季节的深处,听花与黑夜唱尽梦魇,唱尽繁华,唱断所有记忆的 ...

  8. ogg 12c OGG-01163

    数据同步报错: 2017-07-03 12:44:36 ERROR OGG-01163 Oracle GoldenGate Delivery for Oracle, rora_t1.prm: Bad ...

  9. 第二步 (仅供参考) sencha touch + PhoneGap(cordova 2.9 及其以下版本) 使用 adt eclipse进行打包

    首先你得安装一个adt-eclipse 参考资料 http://www.crifan.com/android_eclipse_offline_install_adt/ 然后就可以运行adt-eclip ...

  10. mybatis generator如何定制JavaTypeResolver,使smallint类型的数据库字段在po中的类型为Integer?

    一.问题概述 忙了一段时间的jenkins持续集成,又要开始开发任务了.这两天在用mybatis generator来逆向生成dao层工程. 其中一个问题在于,组长在设计表的时候,不少枚举使用了sma ...