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实例的更多相关文章

  1. Hessian Servlet实例

    Servlet实例 业务场景 在下面的例子中我会发布一个简单的输出字符串的方法,然后在客户端调用并输出结果. 服务器端 环境搭建 在服务端,我们需要引入hessian和servlet的包.编写服务.配 ...

  2. WebService另一种轻量级实现—Hessian 学习笔记

    最近和同事聊天,得知他们在使用一种叫做Hessian的WebService实现方式实现远 程方法调用,是轻量级的,不依赖JavaEE容器,同时也是二进制数据格式传输,效率比SOAP的XML方式要高.感 ...

  3. 最近学习工作流 推荐一个activiti 的教程文档

    全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...

  4. Spring+Hessian+Maven+客户端调用实例

    Hessian是一个采用二进制格式传输的服务框架,相对传统soap web service,更轻量,更快速.官网地址:http://hessian.caucho.com/ 先上个效果图,在客户端界面通 ...

  5. Hessian实例

    简述Hessian Hessian是一个由Caucho Technology开发的轻量级RPC框架,由于它使用二进制RPC协议,所以它更快.更简单,很适合于发送二进制数据(访问官网): 在进行基于He ...

  6. Hessian最佳实践

    前言:本文主要介绍‘独立的Hessian技术’与‘结合Spring技术’的两种Hessian接口开发模式及代码示例. 一.独立的Hessian技术开发步骤 Hessian-Java服务器端必须具备以下 ...

  7. Java学习之Hessian通信基础

    一.首先先说Hessian是什么?    Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简 ...

  8. JAVA上百实例源码以及开源项目

    简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级.中级.高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情.执着,对IT的憧憬. ...

  9. Spring单实例、多线程安全、事务解析

    原文:http://blog.csdn.net/c289054531/article/details/9196053 引言:     在使用Spring时,很多人可能对Spring中为什么DAO和Se ...

随机推荐

  1. wp7图片上传服务器

    做一个wp7手机上传图片到服务器的功能,具体丝路是在手机端做一个照相或者选择图片的功能,点击上传,在服务器端做一个一般处理程序,接受上传的文件,存入文件夹,下面是主要代码: 手机端代码: /// &l ...

  2. 使用Caffe预测遇到的问题

    1. 在使用网络预测图像时, prediction = net.predict( [input_image] ) 出现: net.image_dims[0] 不是整数情况, (2).甚至以为np.ze ...

  3. JS 垃圾回收机制

    [转自]:https://segmentfault.com/a/1190000018605776 垃圾回收 JavaScript 中的内存管理是自动执行的,而且是不可见的.我们创建基本类型.对象.函数 ...

  4. ZBrush的双十一来了,然鹅...

    不管是“光棍节”还是“剁手节” 似乎和我都没有什么关系 事实证明,我错了 今早竟然有不识趣的人发红包祝我单身快乐 纳尼,这是唱的哪一出? 我能直接怼回去,说不领么? 但好像又不是我的风格 哎,一个红包 ...

  5. 优动漫PAINT(clip studio paint)怎么画一幅水墨竹子图

    今天小编分享使用优动漫PAINT绘制一个水墨竹子教程,绘画的过程中我只用到了两个笔刷,即钢笔模式下的“美术字”和“效果线专用”,并且全程鼠标绘制哦,所以生疏的笔触效果大家见谅,没有数位板的小伙伴不妨试 ...

  6. 传入class、id name 的函数封装

    function chooseDate(idName){ 2 $('#' + idName).click(function(){ //执行函数 4 }); 5 }; 6 //传入的 dataOne 就 ...

  7. PHP Base64 加密 & 解密

    <?php 加密: $cany = 'getshell.top'; #定义要加密的字符串 echo base64_encode($cany); #输出加密后的字符串 解密: $cany = 'Z ...

  8. Shader渲染动画

    一.概述 先看一下Shader类的介绍: /** * Shader is the based class for objects that return horizontal spans of col ...

  9. 微信小程序打开PDF

    具体情况是:微信小程序打开springboot返回的pdf文件.微信端先downloadFile,然后openDocument.但是打开文档一直不成功.后来发现官网的例子没有加fileType,我在参 ...

  10. 如何批量导入excel数据至数据库(MySql)--工具phpMyAdmin

    之前由于数据储存使用excel保存了所有数据,经过初步数据筛选,数据量近4000条.一条一条录入数据库显然是不可行的.以下是我所操作的步骤: 1.只保留excel的数据部分,去除第一行的具体说明 2. ...