您的位置:首页 > 编程语言 > Java开发

Spring与Hessian的整合学习

2014-07-11 11:42 260 查看
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

注意事项

在进行基于Hessian的项目开发时,应当注意以下几点:

JAVA服务器端必须具备以下几点:

·包含Hessian的jar包

·设计一个接口,用来给客户端调用

·实现该接口的功能

·配置web.xml,配好相应的servlet

·对象必须实现Serializable 接口

·对于复杂对象可以使用Map的方法传递

客户端必须具备以下几点:

·java客户端包含Hessian.jar的包。C#中引用hessianCSharp.dll

·具有和服务器端结构一样的接口。包括命名空间都最好一样

·利用HessianProxyFactory调用远程接口。

示例:(通过Servlet开发hessian服务器端)

1、jar

<dependency>

<groupId>com.caucho</groupId>

<artifactId>hessian</artifactId>

<version>4.0.7.1</version>

</dependency>

2、api工程,接口定义

package com.chj.api;

public interface HelloService {

public String sysHello(String name);

}

3、hessian工程

3.1 接口实现类

package com.chj.api.impl;

import com.caucho.hessian.server.HessianServlet;

import com.chj.api.HelloService;

public class HelloServiceImpl extends HessianServlet implements HelloService{

private static final long serialVersionUID = 1L;

@Override

public String sysHello(String name) {

return "Hello , " + name;

}

}

3.2 web.xml配置

<servlet>

<servlet-name>hello</servlet-name>

<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>

<init-param>

<param-name>service-class</param-name>

<param-value>com.chj.api.impl.HelloServiceImpl</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>hello</servlet-name>

<url-pattern>/hello</url-pattern>

</servlet-mapping>

示例:(通过Servlet开发hessian服务器端)

写Spring的发布Hessian服务的配置文件

接口定义:

package com.chj.api;

public interface Test1Service {

public String sysHello(String name);

}

接口实现:

package com.chj.api.impl;

import org.springframework.stereotype.Service;

import com.chj.api.Test1Service;

@Service("test1Service")

public class Test1ServiceImpl implements Test1Service {

@Override

public String sysHello(String name) {

return "Hello , " + name;

}

}

文件:spring-hessian.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 开启bean类的注解支持 (让 @Autowired等注解工作起来 ) -->

<context:annotation-config />

<!-- 自动扫描指定的包下面的通过注解标识的组件,无需使用xml来定义spring的bean了 -->

<context:component-scan base-package="com.chj" />

<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

<bean name="/test1" class="org.springframework.remoting.caucho.HessianServiceExporter">

<property name="service" ref="test1Service" />

<property name="serviceInterface" value="com.chj.api.Test1Service" />

</bean>

</beans>

文件:hessian-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Hessian -->

<import resource="classpath:spring-hessian.xml" />

</beans>

文件:web.xml

<!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>

<servlet>

<servlet-name>hessian</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>4</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>hessian</servlet-name>

<url-pattern>/hessian/*</url-pattern>

</servlet-mapping>

</web-app>

主要事项:

a)hessian-servlet.xml的文件名必须以<servlet-name>hessian</servlet-name>名字开头,并且加上-servlet.xml一段,组成完整的文件名。

b)hessian-servlet.xml的文件名格式必须是[servlet-name]-servlet.xml格式,否则出错。

c) hessian-servlet.xml需要放在WEB-INF目录下

测试示例:

1.java客户端调用

HessianProxyFactory factory = new HessianProxyFactory();

String url = "http://localhost:8888/hessian/hello";

HelloService basic = (HelloService) factory.create(HelloService.class, url);

String name = "derek";

System.out.println(basic.sysHello(name));

2.与Spring整合使用

<!-- 客户端Hessian代理工厂Bean -->

<bean id="helloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">

<!-- 请求代理Servlet路径 -->

<property name="serviceUrl">

<value>http://localhost:8888/hessian/hello</value>

</property>

<!-- 接口定义 -->

<property name="serviceInterface">

<value>com.chj.api.HelloService</value>

</property>

</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: