---
title: "Hessian应用"
author: "fansofjava"
date: "2011-05-04T17:11:54Z"
tags: []
categories: ["编程语言"]
slug: "hessian"
draft: false
lastmod: "2011-05-04T17:23:57Z"
id: 1028520
url: "https://fansofjava.iteye.com/blog/1028520"
---

hessian是一种基于二进制协议传输的分布式组件。因为基于二进制协议而不是像webservice那样，基于xml，所以传输效率会高一些。
虽然不太明白其中的原理，但用java开发的hessian在.net下也是可以调用的， 即可以说实现跨平台。这方面的例子在网上也很少，所以决定花些时间整理一下，本人对hessian的了解也很有限。
hessian有许多版本，大的版本有两个，spring2.x支持hessian3，spring3.x支持hessian4。本例子将采用ibatis2+springmvc3+spring3+hessian4开发。下面列出使用了hessian需要注意的地方。
1.实体类必须实现Serializable接口。

2.配置hessian服务端.

```xml

<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务--> 
    <bean id="HessianRemoting" name="/HessianRemoting" class="org.springframework.remoting.caucho.HessianServiceExporter"> 
           <!--  需要导出的目标bean--> 
           <property name="service" ref="depService"/> 
           <!--  Hessian服务的接口--> 
           <property name="serviceInterface" value="com.adtech.service.DepService"/> 
    </bean> 
    
    <bean id="hessianMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">			
			<props>
			  <prop key="/services">HessianRemoting</prop>
			</props>
		</property>
	</bean>

```

这个文件的文件名默认为remoting-servlet.xml，放在web-inf目录下。

3.客户端配置：


```xml

<bean id="myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> 
		<property name="serviceUrl"> 
			<value>http://localhost:8080/hessianService/remoting/services</value> 
		</property> 
		<property name="serviceInterface"> 
			<value>com.adtech.service.DepService</value> 
		</property> 
	</bean> 

```


4.客户端测试如下：

```java

        @Before
	public void setUp() {
		context = new ClassPathXmlApplicationContext("remoting-client.xml");
	}

	@Test
	public void getAllDep() {
		DepService depService = (DepService) context.getBean("myServiceClient");
		System.out.println(depService.getAllDeps());
	}
```

从上面过程可以看出，其实在spring下开发hessian服务是一件相当简单的事。