Hessian Spirng实例
Spring实例
之前,我们做了很简单的纯Hessian的调用,虽然到此已经能够满足远程调用的需求了,但是我听说spring也能够访问hessian的远程服务,研究了一番,废话不多说,直接上示例。
业务场景
servlet的例子并未涉及到复杂对象的传输,这次我们搞复杂点,设计一个服务,通过远程调用的方式来找爸爸的儿子。
服务端
环境搭建
引入hessian、spring-mvc的相关jar包,后面会附上相关的pom文件配置,项目结构如下:

示例代码
复杂的对象传输时,只需要类继承Serializable,保证在数据传输时能够序列化和反序列化,如下面的Father和Child类。
父亲:
package example;
import java.io.Serializable;
/**
* @author X
*/
public class Father implements Serializable {
private static final long serialVersionUID = 1L;
public Father(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
}
Father.java
儿子:
package example;
import java.io.Serializable;
/**
* @author X
*/
public class Child implements Serializable {
private static final long serialVersionUID = 1L;
public Child(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
}
Child.java
接送接口:
package example;
/**
* @author X
*/
public interface ShuttleService {
String getCar();
Child getChild(Father father);
}
ShuttleService.java
接送实现:
package example;
/**
* @author X
*/
public class ShuttleServiceImpl implements ShuttleService {
public String getCar() {
return "小火车";
}
public Child getChild(Father father) {
if (father != null)
return new Child(father.getName() + "的儿子");
return null;
}
}
ShuttleServiceImpl.java
spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="shuttle" class="example.ShuttleServiceImpl"/>
<bean name="/shuttleService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service">
<ref bean="shuttle"/>
</property>
<property name="serviceInterface">
<value>example.ShuttleService</value>
</property>
</bean>
</beans>
hessian-spring.xml
web配置:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Demo</display-name>
<servlet>
<servlet-name>shuttle</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/hessian-spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>shuttle</servlet-name>
<url-pattern>/rpc/*</url-pattern>
</servlet-mapping>
</web-app>
web.xml
依赖配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>container</artifactId>
<groupId>hessian.host</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring</artifactId>
<packaging>war</packaging>
<name>spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>spring</finalName>
</build>
pom.xml
客户端
环境搭建
引入hessian、spring-mvc的相关jar包,项目结构如下:

调用:
import example.Father;
import example.ShuttleService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.net.MalformedURLException;
/**
* @author X
*/
public class Run {
public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-rpc.xml");
ShuttleService ss = (ShuttleService) context.getBean("rpcClient");
System.out.println(ss.getCar());
System.out.println(ss.getChild(new Father("王老二")).getName());
}
}
Run.java
客户端spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="rpcClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:45/rpc/shuttleService</value>
</property>
<property name="serviceInterface">
<value>example.ShuttleService</value>
</property>
</bean>
</beans>
spring-rpc.xml
运行后输出结果如下:

注意在实例中我是把Father和Child和ShuttleService分别定义了两次,实际开发中不建议这样做,把需要调用的部分作为一个公共API供分别提供给客户端和服务端使用,否则可能会照成反序列化失败。
把spring和hessian相结合后,无论是服务端还是客户端的业务代码中,已经没有一行与hessian相关的代码了,也就是说spring让我们的开发无关远程接口的实现了,这样我们就可只关注于开发而不必去关注远程调用怎么去实现。如果说我们哪天需要切换另外一个spring支持的远程访问接口,也只需要修改下配置文件就搞定了,so easy,妈妈再也不用担心我的实现代码了!
Hessian Spirng实例的更多相关文章
- Hessian Servlet实例
Servlet实例 业务场景 在下面的例子中我会发布一个简单的输出字符串的方法,然后在客户端调用并输出结果. 服务器端 环境搭建 在服务端,我们需要引入hessian和servlet的包.编写服务.配 ...
- WebService另一种轻量级实现—Hessian 学习笔记
最近和同事聊天,得知他们在使用一种叫做Hessian的WebService实现方式实现远 程方法调用,是轻量级的,不依赖JavaEE容器,同时也是二进制数据格式传输,效率比SOAP的XML方式要高.感 ...
- 最近学习工作流 推荐一个activiti 的教程文档
全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...
- Spring+Hessian+Maven+客户端调用实例
Hessian是一个采用二进制格式传输的服务框架,相对传统soap web service,更轻量,更快速.官网地址:http://hessian.caucho.com/ 先上个效果图,在客户端界面通 ...
- Hessian实例
简述Hessian Hessian是一个由Caucho Technology开发的轻量级RPC框架,由于它使用二进制RPC协议,所以它更快.更简单,很适合于发送二进制数据(访问官网): 在进行基于He ...
- Hessian最佳实践
前言:本文主要介绍‘独立的Hessian技术’与‘结合Spring技术’的两种Hessian接口开发模式及代码示例. 一.独立的Hessian技术开发步骤 Hessian-Java服务器端必须具备以下 ...
- Java学习之Hessian通信基础
一.首先先说Hessian是什么? Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简 ...
- JAVA上百实例源码以及开源项目
简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级.中级.高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情.执着,对IT的憧憬. ...
- Spring单实例、多线程安全、事务解析
原文:http://blog.csdn.net/c289054531/article/details/9196053 引言: 在使用Spring时,很多人可能对Spring中为什么DAO和Se ...
随机推荐
- Teamwork-六月上旬心得体会
六月上旬心得体会 在五月末的时候,老师针对我们团队的状况提出了几点建议和解决方案,而这半个月里,我们尝试性地运用了其中的几件工具与方法. 1.燃尽图与每日总结 我们采用的是<构建之法>书中 ...
- 实验6 Bezier曲线生成
1.实验目的: 了解曲线的生成原理,掌握几种常见的曲线生成算法,利用VC+OpenGL实现Bezier曲线生成算法. 2.实验内容: (1) 结合示范代码了解曲线生成原理与算法实现,尤其是Bezier ...
- 杭电2061WA
#include<stdio.h> struct mem { char s[50]; int c; int f; }; int main() { struct mem x[60]; int ...
- 对服务器磁盘、CPU、内存使用状态,设置163邮件告警
1,桥接模式可上网,首先你的邮箱已经开通yum -y install mailx dos2unix.x86_64 mailx -V[root@localhost ~]# vim /etc/mail. ...
- 历年真题 未完成(Noip 2008 - Noip 2017)
Noip 2008 :全部 Noip 2009 :全部 Noip 2010 :AK Noip 2011 :AK Noip 2012 : Vigenère 密码,国王游戏,开车旅行 Noip 2013 ...
- express+node.js搭建的服务器和在sublimeServer下的页面请求报跨域错误
1.前端页面使用vue中的axios请求nodejs响应.报以下错误: Failed to load http://localhost:3000/users/validate: Response to ...
- nginx获取经过层层代理后的客户端真实IP(使用正则匹配)
今天帮兄弟项目搞了一个获取客户端真实IP的问题,网上这种问题很多,但是对于我们的场景都不太合用,现把我的解决方案share给大家,如有问题,请及时指出. 场景: 在请求到达后端服务之前,会经过层层代理 ...
- Python笔记7----Pandas中变长字典Series
1.Series概念 类似一维数组的对象,由数据和索引组成 2.Series创建 用Series()函数创建,0,1,2为series结构自带的索引. 可以自己指定索引值,用index,也可以直接用字 ...
- python类的内置attr属性
class Foo: x=1 def __init__(self,y): self.y=y def __getattr__(self, item): print('----> from geta ...
- 会话cookie和持久化cookie实现session
当你第一次访问一个网站的时候,网站服务器会在响应头内加上Set- Cookie:PHPSESSID=nj1tvkclp3jh83olcn3191sjq3(php服务器),或Set-Cookie JSE ...