`

Hessian与Spring集成

阅读更多

一、服务端

1、所需jar包(Spring相关jar包、hessian-4.0.37.jar

2、服务端代码

 2.1、实体对象

package com.hessian.spring.entity;

import java.io.Serializable;

public class User implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private String userName;
	private String password;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

  2.2、接口

package com.hessian.spring;

import java.util.List;
import java.util.Map;

import com.hessian.spring.entity.User;

public interface IHello {
	public String sayHello(String name);
	public String getUserList(List<User> users);
	public String getUserMap(Map<String, User> maps);
}

  2.3、实现类

package com.hessian.spring.impl;

import java.util.List;
import java.util.Map;

import com.hessian.spring.IHello;
import com.hessian.spring.entity.User;

public class IHelloImpl implements IHello {

	public String sayHello(String name) {
		return "Hello," + name;
	}

	public String getUserList(List<User> users) {
		StringBuffer stringBuffer = new StringBuffer();
		for (User user : users) {
			stringBuffer.append("[");
			stringBuffer.append(user.getUserName());
			stringBuffer.append("--");
			stringBuffer.append(user.getPassword());
			stringBuffer.append("]");
		}
		return stringBuffer.toString();
	}
	
	public String getUserMap(Map<String, User> maps){
		StringBuffer stringBuffer = new StringBuffer();
		for(String key : maps.keySet()){
			stringBuffer.append("[");
			stringBuffer.append(maps.get(key).getUserName());
			stringBuffer.append("--");
			stringBuffer.append(maps.get(key).getPassword());
			stringBuffer.append("]");
		}
		return stringBuffer.toString();
	}
}

 2.4、配置Web.xml

<!--   Hessian集成Spring-->
<servlet>
	<servlet-name>remote</servlet-name>
	<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>

 2.5、配置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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 接口的具体实现类 -->  
    <bean id="iHello" class="com.hessian.spring.impl.IHelloImpl" />  
    <!-- 使用Spring的HessianServie做代理 -->  
    <bean name="/HelloSpring" class="org.springframework.remoting.caucho.HessianServiceExporter">  
        <!-- service引用具体的实现实体Bean-->  
        <property name="service" ref="iHello" />  
        <property name="serviceInterface" value="com.hessian.spring.IHello" />  
    </bean>  
</beans>

 注:

      这个文件为什么叫remote-servlet.xm

     在web.xml中有配置:<servlet-name>remote</servlet-name>

     所以remote-servlet.xml的文件名必须以

 

 

二、客户端

1、配置客户端 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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <!-- 客户端Hessian代理工厂Bean -->
	<bean id="helloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<!-- 请求代理Servlet路径 -->
		<property name="serviceUrl">
			<value>http://127.0.0.1:8080/remote/HelloSpring</value>
		</property>
		<!-- 接口定义 -->
		<property name="serviceInterface">
			<value>com.hessian.spring.IHello</value>
		</property>  
	</bean>
</beans>

 2、客户端测试类

package com.hessian.spring.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.hessian.spring.IHello;
import com.hessian.spring.entity.User;

public class SpringClientTest {

	public static String url = "http://127.0.0.1:8080/remote/HelloSpring";

	public static void main(String[] args) {
		try {
			ApplicationContext contex = new ClassPathXmlApplicationContext("remote-client.xml");  
	  
	        // 获得客户端的Hessian代理工厂bean  
	        IHello iHello = (IHello) contex.getBean("helloService");
			System.out.println(iHello.sayHello("tzz"));
			User user1 = new User();
			user1.setUserName("a1");
			user1.setPassword("123456");
			User user2 = new User();
			user2.setUserName("a2");
			user2.setPassword("123456");
			List<User> users = new ArrayList<User>();
			users.add(user1);
			users.add(user2);
			System.out.println(iHello.getUserList(users));
			Map<String, User> maps = new HashMap<String, User>();
			maps.put("user1", user1);
			maps.put("user2", user2);
			System.out.println(iHello.getUserMap(maps)); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

分享到:
评论

相关推荐

    Hessian和Spring集成示例

    这是Hessian和Spring集成的示例,具体步骤请参考本人博客: http://blog.csdn.net/jjasun/article/details/29593083

    Spring集成Hessian案例

    Spring集成Hessian案例

    Hessian(Spring集成的)的应用与研究

    NULL 博文链接:https://zwustudy.iteye.com/blog/1611651

    spring 集成 hessian例子

    Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。参考文档地址...

    Spring中集成Hessian的问题

    NULL 博文链接:https://wo-niu.iteye.com/blog/2200720

    一个简单的Hessian示例

    一个简单的Hessian,简单介绍了Hessian的使用方式,介绍了Hessian和Spring集成的使用方式,以及单独使用Hessian的方式。

    spring集成Hessian插件配置改成自动注入。

    NULL 博文链接:https://xiaofancn.iteye.com/blog/2356498

    hessian学习实例

    hessian学习实例,hessian框架例子,与Spring集成。包括了server端和client端

    hessian3.0.1.jar

    hessian3.0.1.jar, 可以和spring1.x集成,不会报错

    hessian-demo示例

    hessian-demo工程示例,已集成内置jetty插件,可以直接运行。 spring4,hessian4.0.7

    Hessian 使用小结

    Hessian 是一种轻量级的二进制RPC通讯框架,基于HTTP使用servlet 暴漏web service. 本文描述了单独使用和及spring集成使用,并介绍了证书加密,签名及非证书类如DES加密方式. 文尾附官方实例链接.

    spring jar 包详解

    (11) spring-web.jar 这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、 Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 (12) ...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类,以及校验Validation方面的相关类。...

    最新最全的spring开发包

    可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类,以及校验Validation方面的相关类。...

    Spring 实现远程访问详解——rmi

    Spring为各种远程访问技术提供集成工具类。Spring远程访问通过使用普通POJOs,能更容易的开发远程访问服务。目前,Spring远程访问的主要技术如下: 1. 远程调用RMI(Remote Method Invocation): 通过使用 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    13.1.1. 与其他web框架的集成 13.1.2. Spring Web MVC框架的特点 13.2. DispatcherServlet 13.3. 控制器 13.3.1. AbstractController 和 WebContentGenerator 13.3.2. 其它的简单控制器 13.3.3. ...

    Spring 2.0 开发参考手册

    17. 使用Spring进行远程访问与Web服务 17.1. 简介 17.2. 使用RMI暴露服务 17.2.1. 使用 RmiServiceExporter 暴露服务 17.2.2. 在客户端链接服务 17.3. 使用Hessian或者Burlap通过HTTP远程调用服务 17.3.1. 为...

    Spring中文帮助文档

    13.1.1. 与其他MVC实现框架的集成 13.1.2. Spring Web MVC框架的特点 13.2. DispatcherServlet 13.3. 控制器 13.3.1. AbstractController 和 WebContentGenerator 13.3.2. 其它的简单控制器 13.3.3. ...

    spring chm文档

    17. 使用Spring进行远程访问与Web服务 17.1. 简介 17.2. 使用RMI暴露服务 17.2.1. 使用 RmiServiceExporter 暴露服务 17.2.2. 在客户端链接服务 17.3. 使用Hessian或者Burlap通过HTTP远程调用服务 17.3.1. 为...

    Spring API

    13.1.1. 与其他MVC实现框架的集成 13.1.2. Spring Web MVC框架的特点 13.2. DispatcherServlet 13.3. 控制器 13.3.1. AbstractController 和 WebContentGenerator 13.3.2. 其它的简单控制器 13.3.3. ...

Global site tag (gtag.js) - Google Analytics