Spring2 提供的remote包学习笔记

2024-01-26 20:32

本文主要是介绍Spring2 提供的remote包学习笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring2 提供的remote包学习笔记

Spring2 针对远程访问服务,提供的一个remote包。其的的是提供一套统一的远程服务发布功能。
先来看一下Spring2支持那些远程服务功能:
    1. RMI服务
    2. Hessian或者Burlap通过HTTP远程调用服务
    3. HTTP调用器暴露服务

下面用一个例子,来看一下Spring2 是怎样对这些服务进行统一的封装和管理。

先看一下服务器端的源代码

public   interface  IBookService {

    Book getById(String id);

}

public   class  Book {

    
public  String name;
    
public  String id;
    
public  String author;

}
    
public   class  BookService  implements  IBookService {

    
public  Book getById(String id) {
        
return  BookStore.getById(id);
    }

}   


客户端源代码

public   class  BookQueryService {
  
private  IBookService bookService;
  
public   void  setAccountService(IBookService bookService) {
    
this .bookService  =  bookService;
  }
  
  
public  Book getBookById(String id) {
      
return  bookService.getById(id);
  }
}

// 客户端调用示例

public   static   void  main(String[] args) {

  ClassPathXmlApplicationContext context;
    context 
=   new   ClassPathXmlApplicationContext( " applicationContext.xml " );
    BookQueryService bookQueryService 
=  (BookQueryService) context.getBean( " bookQueryService " );
    Book book 
=  bookQueryService.getBookById( " 1 " );
}


使用Spring2 发布 RMI服务示例

服务器端配置:
< bean  id ="bookService"  class ="com.xmatthew.spring.remote.BookService" >
</ bean >

< bean  class ="org.springframework.remoting.rmi.RmiServiceExporter" >
    
<!--  does not necessarily have to be the same name as the bean to be exported  -->
    
< property  name ="serviceName"  value ="bookService" />
    
< property  name ="service"  ref ="bookService" />
    
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
    
< property  name ="registryPort"  value ="1800" />
</ bean >

客户端配置:

< bean  class ="com.xmatthew.spring.remote.client.BookQueryService" >
    
< property  name ="bookService"  ref ="bookService" />
</ bean >

< bean  id ="bookService"  class ="org.springframework.remoting.rmi.RmiProxyFactoryBean" >
    
< property  name ="serviceUrl"  value ="rmi://localhost:1800/bookService" />
    
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >


 使用Spring2 发布 基于Http的Hessian服务示例
 注: Hessian提供一种基于HTTP的二进制远程协议。它是由Caucho创建的,可以在 http://www.caucho.com 找到更多有关Hessian的信息。
 
 首为使用Hessian,需要为其配置Spring 的 DispatcherServlet
 把下面的配置加入到web.xml中

  < servlet >
    
< servlet-name > remoting </ servlet-name >
    
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
    
< load-on-startup > 1 </ load-on-startup >
</ servlet >

< servlet-mapping >
    
< servlet-name > remoting </ servlet-name >
    
< url-pattern > /remoting/* </ url-pattern >
</ servlet-mapping >

 

服务器端配置:
< bean  id ="bookService"  class ="com.xmatthew.spring.remote.BookService" >
</ bean >

< bean  name ="/bookService"  class ="org.springframework.remoting.caucho.HessianServiceExporter" >
  
< property  name ="service"  ref ="bookService" />
  
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >

客户端配置:

< bean  class ="com.xmatthew.spring.remote.client.BookQueryService" >
    
< property  name ="bookService"  ref ="bookService" />
</ bean >

< bean  id ="bookService"  class ="org.springframework.remoting.caucho.HessianProxyFactoryBean" >
    
< property  name ="serviceUrl"  value ="http://localhost:8080/bookService" />
    
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >


使用Spring2 发布 基于Http的Burlap服务示例

 Burlap,它是一个基于XML的Hessian替代方案。它的配置方法和上述Hessian的一样。只要把 Hessian 换成 Burlap 就行了。
 服务器端使用:
     org.springframework.remoting.caucho.BurlapServiceExporter 发布服务
 客户端使用:
     org.springframework.remoting.caucho.BurlapProxyFactoryBean

使用Spring2 发布 基于HTTP调用器暴露服务

和使用自身序列化机制的轻量级协议Burlap和Hessian相反,Spring HTTP调用器使用标准Java序列化机制来通过HTTP暴露业务.
但其配置与Burlap和Hessian很相近

服务器端配置:
< bean  id ="bookService"  class ="com.xmatthew.spring.remote.BookService" >
</ bean >

< bean  name ="/bookService"  class ="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" >
  
< property  name ="service"  ref ="bookService" />
  
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >

客户端配置:

< bean  class ="com.xmatthew.spring.remote.client.BookQueryService" >
    
< property  name ="bookService"  ref ="bookService" />
</ bean >

< bean  id ="bookService"  class ="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" >
    
< property  name ="serviceUrl"  value ="http://localhost:8080/bookService" />
    
< property  name ="serviceInterface"  value ="com.xmatthew.spring.remote.IBookService" />
</ bean >



这篇关于Spring2 提供的remote包学习笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/647980

相关文章

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

springboot项目redis缓存异常实战案例详解(提供解决方案)

《springboot项目redis缓存异常实战案例详解(提供解决方案)》redis基本上是高并发场景上会用到的一个高性能的key-value数据库,属于nosql类型,一般用作于缓存,一般是结合数据... 目录缓存异常实践案例缓存穿透问题缓存击穿问题(其中也解决了穿透问题)完整代码缓存异常实践案例Red

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操