hessian与spring的结合

对于EJB将被取代的说法已经比比皆是了,虽然我不认同吧,但是实话实说对于大多数的项目来说使用企业级的java bean还真的是没什么必要,因为太重量级了,上篇博客不是说了一下简单的hessian吗,这里就来说说hessian和spring的结合。

1、引入jar包

需要的jar包有:

2、配置remote-servlet.xml文件(在src目录下)

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 接口的具体实现类 -->
    <bean id="impl" class="com.tgb.hessian.service.impl.HelloServiceImpl" />
    <!-- 使用Spring的HessianServie做代理 -->
    <bean name="/helloSpring"
    class="org.springframework.remoting.caucho.HessianServiceExporter">
        <!-- service引用具体的实现实体Bean-->
        <property name="service" ref="impl" />
        <property name="serviceInterface" value="com.tgb.hessian.service.HelloServiceSpring" />
    </bean>

    <!-- 可以配置多个HessianServiceExporter代理Bean -->
</beans>

3、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Spring-Hessian</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
<servlet>
    <servlet-name>remote</servlet-name>
    <!-- 使用Spring的代理Servlet -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>namespace</param-name>
        <param-value>classes/remote-servlet</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>  

<servlet-mapping>
    <servlet-name>remote</servlet-name>
    <url-pattern>/remote/*</url-pattern>
</servlet-mapping>
</web-app>  

4、服务接口和 接口的实现

(1)接口

package com.tgb.hessian.service;
public interface HelloServiceSpring {

    /**
     * hellworld 服务类接口
     * @author 陈丽娜
     * @version 2015年7月26日下午6:18:41
     * @return
     */
    public String HellowWorld(String name);
}

(2)实现

package com.tgb.hessian.service.impl;
import com.tgb.hessian.service.HelloServiceSpring;
/**
 * 服务实现类
 * @author 陈丽娜
 * @version 2015年7月26日下午9:17:03
 */
public class HelloServiceImpl implements HelloServiceSpring {
    @Override
    public String HellowWorld(String name) {
        // TODO Auto-generated method stub
        return "hello" + name;
    }
}

5、客户端的相关配置(同样需要引入spring的jar包和hessian的jar包,还有服务接口的jar。这里就不再赘述)

remote-client.xml配置

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 客户端Hessian代理工厂Bean -->
    <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <!-- 请求代理Servlet路径 -->
        <property name="serviceUrl">
         <value>http://localhost:8080/Spring-Hessian/remote/helloSpring</value>
        </property>
        <!-- 接口定义 -->
        <property name="serviceInterface">
            <value>com.tgb.hessian.service.HelloServiceSpring</value>
        </property>
    </bean>
</beans>  

6、测试类的编写

package com.tgb.hessian.test;

import java.net.MalformedURLException;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tgb.hessian.service.HelloServiceSpring;

public class TestHessianSpring {
	    @Test
		public void  Test1() throws MalformedURLException {
	    	ApplicationContext contex = new ClassPathXmlApplicationContext(
	                "remote-client.xml");  

	        // 获得客户端的Hessian代理工厂bean
	        HelloServiceSpring i = (HelloServiceSpring) contex.getBean("clientSpring");
	        System.out.println(i.HellowWorld("陈丽娜"));
		}

}

7、运行结果:

是不是很简洁呢?!赶快学起来吧!

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 00:08:47

hessian与spring的结合的相关文章

Hessian与Spring集成

简介: Hessian是一个简单的连接Web服务的二进制协议. 客户端和服务端不依赖于其他任何jar,比起webService 它显得轻量许多,比如使用xfire包含核心库和客户端的jar,大小达到了10M ,而最新的hessian-4.0.7  jar大小也只有不到400K. 更适合二进制的传输,比起webService.Hessian的 传输速度要高于webService. 支持Java,c#,Flex(actionscrpit) 配置: Hessian的访问分为客户端和服务端,首先都要有H

Hessian入门(包括与Spring集成)

A.纯Hessian接口开发步骤 Hessian-JAVA服务器端必须具备以下几点: * 1.包含Hessian的jar包(hessian-4.0.37.jar) * 2.设计一个接口,用来给客户端调用(IHessian.java) * 3.实现该接口的功能(IHessianImpl.java) * 4.配置web.xml,配好相应的Servlet(web.xml) * 5.对象必须实现Serializable接口(Foo.java) Hessian-JAVA服务端代码预览图: 1.包含Hess

Hessian Servlet和Hessian Spring的简单应用

转自: http://lancui.iteye.com/blog/935578 简介 相比WebService,Hessian更简单.快捷.采用的是二进制RPC协议(Binary),因为采用的是二进制协议,所以它很适合于发送二进制数据.Hessian通常通过Web应用来提供服务,因此非常类似于WebService.只是它不使用SOAP协议. Hessian通过Servlet提供远程服务.需要将匹配某个模式的请求映射到Hessian服务.Spring的DispatcherServlet可以完成该功

Hessian原理分析

一.      远程通讯协议的基本原理 网络通信需要做的就是将流从一台计算机传输到另外一台计算机,基于传输协议和网络 IO 来实现,其中传输协议比较出名的有 http . tcp . udp 等等, http . tcp . udp 都是在基于 Socket 概念上为某类应用场景而扩展出的传输协议,网络 IO ,主要有 bio . nio . aio 三种方式,所有的分布式应用通讯都基于这个原理而实现,只是为了应用的易用,各种语言通常都会提供一些更为贴近应用易用的应用层协议. 二.      应

[转载] Java学习之Hessian通信基础

转载自http://blog.sina.com.cn/s/blog_7f73e06d0100xn9j.html 一.首先先说Hessian是什么?    Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简单.快捷.采用的是二进制RPC协议,因为采用了二进制协议,所以它很适合于发送二进制数据,Hessian主要作面向对象的消息通信.Hessian的初衷就是支持动态类型,格式紧凑,跨语言Hes

Java学习之Hessian通信基础

一.首先先说Hessian是什么? Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简单.快捷.采用的是二进制RPC协议,因为采用了二 进制协议,所以它很适合于发送二进制数据,Hessian主要作面向对象的消息通信.Hessian的初衷就是支持动态类型,格式紧凑,跨语言 Hessian是使用自己的序列化机制实现的编组和反编组,其支持的数据类型是有限制的,不支持复杂的对象,可以穿透防火墙,在

hessian入门

hessian简介 Hessian是二进制的web service协议,官方网站提供Java.Flash/Flex.Python.C++..NET C#等实现.Hessian和Axis.XFire都能实现web service方式的远程方法调用,区别是Hessian是二进制协议,Axis.XFire则是SOAP协议,所以从性能上说Hessian远优于后两者,并且Hessian的JAVA使用方法非常简单.Hessian由于没有WSDL这种服务描述文件去对实现进行规定,似乎更适合内部分布式系统之间的

com.caucho.hessian.io.HessianProtocolException: expected string at 0x6d

1. 现象 在学习Hessian时,A系统通过hessian去调用B,但收到500错误: java.io.IOException: Server returned HTTP response code: 500 for URL 调试B,看到标题所述异常. 2. 分析 经过搜索,看到这样的信息: “是hessian和spring的版本不兼容引起的”  http://itlab.idcquan.com/Java/base/849773_4.html 3. 原因 经过分析发现,B所使用的hessian

使用Hessian创建接口

使用Hessian创建接口 Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单.快捷.采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据. 详细介绍 1. Hessian集成Spring配置 加入Hessian依赖 <hessian.version>4.0.38</hessian.version> ... <!-- hessian --> <