服务端:

RmiServer.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"

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

    <!-- RMI远程调用 -->

    <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">

        <property name="serviceName" value="MyRMIService" />

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

        <property name="serviceInterface" value="com.test.ddy.service.IRMIService" />

        <property name="registryPort" value="8088" />

    </bean>

    <bean id="rmiService" class="com.test.ddy.service.RMIServiceImpl" /></beans> 

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">     <description>Spring公共配置</description>     <!-- 自动扫描 -->
    <context:component-scan base-package="com.test.ddy" />
    <mvc:annotation-driven />
    <context:annotation-config />     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>
    <import resource="RmiServer.xml" />
    <import resource="spring-mybatis.xml" />
</beans> 

定义服务接口:

public interface IRMIService {
    String getUserName(String name);

}

定义实现类:

package com.test.ddy.service;

import org.apache.log4j.Logger;

public class RMIServiceImpl implements IRMIService {
    
    private static final Logger logger = Logger.getLogger(RMIServiceImpl.class);
            
    @Override
    public String getUserName(String name) {
        // TODO Auto-generated method stub
        return "hello,"+name;
    }

}

客户端:

RmiServer.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- RMI远程调用 -->
    <bean id="iRMIService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl" value="rmi://127.0.0.1:8088/MyRMIService" />
        <property name="serviceInterface" value="com.test.ddy.service.IRMIService" />
    </bean>
</beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <description>Spring公共配置</description>
    
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.test.ddy" />
    <mvc:annotation-driven />
    <context:annotation-config />
    
    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
    <import resource="spring-mybatis.xml"/>

</beans>

客户端的spring-mvc.xml中不需要引入RmiServer.xml

定义接口:

package com.test.ddy.service;

public interface IRMIService {
    String getUserName(String name);

}

客户端不需要实现这个接口,

客户端Controller:

package com.test.ddy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.test.ddy.model.User;
import com.test.ddy.service.IRMIService;
import com.test.ddy.service.IUserService; /**
 */
@Controller
@RequestMapping("/index")
public class IndexController {     @RequestMapping(value = "/getRMI", method = RequestMethod.GET)
    public String getRMI(HttpServletRequest request) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                "classpath:RmiServer.xml");  
        IRMIService hs = (IRMIService) ctx.getBean("iRMIService");  
        String temp = hs.getUserName("my vincent ");
        System.out.println(temp);
        return "index";
    }

}

客户端控制台输出结果:hi my vincent

如果要传递一个User对象,则需要进行序列化User implements Serializable

服务端:github https://github.com/vincentduan/SpringRMIServer.git

客户端:https://github.com/vincentduan/SpringRMIClient.git

Spring + RMI的更多相关文章

  1. Spring RMI (Spring 远程方法调用)

    所需jar包...?    不纠结,一股脑儿全导! 源码地址:http://pan.baidu.com/s/1jG8eOmy 先放结构图如下,客户端和服务端都在一个项目中.也可以把服务端的xxx导成j ...

  2. Spring RMI (Spring 远程方法调用)【原】

    所需jar包...?    不纠结,一股脑儿全导! 源码地址:http://pan.baidu.com/s/1jG8eOmy 先放结构图如下,客户端和服务端都在一个项目中.也可以把服务端的xxx导成j ...

  3. spring RMI的使用

    Spring整合RMI的原理 客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性. 通过JRMP访问服务.JRMP JRMP:ja ...

  4. Spring RMI Example

    一: 提供服务的远程一端 1-1. applicationContext.xml <?xml version="1.0" encoding="UTF-8" ...

  5. Spring Remoting: Remote Method Invocation (RMI)--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...

  6. Spring之RMI 远程方法调用 (Remote Method Invocation)

    RMI 指的是远程方法调用 (Remote Method Invocation) 1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程 ...

  7. Spring之rmi实例演示

    环境介绍:本文中服务端客户端使用的都是ssm框架,配置文件分为spring_servlet.xml,spring_service.xml,mybatis.xml 在spring里面使用rmi完成远程调 ...

  8. spring源码分析之spring-web remoting模块概况及基本概念

    spring-web总体分为三部分:caucho.httpinvoker.jaxws,其总体构造图如下: uml结构: 先看看网上搜索到的上述实现的原理吧:Spring RMI,Hessian/Bur ...

  9. RMI学习

    前段时间学习JMX,知道可以使用rmi连接器,就顺便看下rmi是什么东西,RMI 全称Remote Method Invocation-远程方法调用,实现远程对象之间的调用,下面原理图来自网络 服务器 ...

随机推荐

  1. UVA 11020 Efficient Solutions (BST,Splay树)

    题意:给n个坐标.一个坐标(x,y)若有无存在的坐标满足x1<x && y1<=y  或  x1<=x && y1<y 时,此坐标(x,y)是就 ...

  2. 合并百度影音的离线数据 with python 2.3 格式更新

    很久没有更新了. 这次新增支持四种格式的解析. filelist slicelist download.cfg third_party_download.cfg 还是2个文件.替换之前版本即可. 初步 ...

  3. golang结构体排序 - 根据下载时间重命名本地文件

    喜M拉Y下载音频到手机,使用ximalaya.exe 解密[.x2m]为[.m4a]根据文件下载创建时间,顺序重命名文件,方便后续播放. 源码如下:package main import ( &quo ...

  4. easyui权限管理

    在easyui上实现权限的管理 所谓权限:指的是系统中的资源,资源包括菜单资源(学习情况报表,账号审核...)以及按钮资源所谓角色:指的是系统中的权限集合(每一个角色对应着哪些权限集合) 1.一星权限 ...

  5. gprc-java与golang分别实现服务端,客户端,跨语言通信(二.golang实现)

    1.编译器protoc, 下载地址:https://github.com/protocolbuffers/protobuf/releases  (下载对应的版本, 解压后放到go的bin中) 2.安装 ...

  6. bzoj 1051 受欢迎的牛-tarjan

    https://www.lydsy.com/JudgeOnline/problem.php?id=1051 如果A喜欢B,那么A->B连边,那么整个图储存下来,如果有好多个牛是受欢迎的,那么他们 ...

  7. [LOJ] #2363「NOIP2016」愤怒的小鸟

    精度卡了一个点,别人自带大常数,我自带大浮点误差qwq. 听了好几遍,一直没动手写一写. f[S]表示S集合中的猪被打死的最少抛物线数,转移时考虑枚举两个点,最低位的0为第一个点,枚举第二个点,构造一 ...

  8. 201621123079《java程序设计》第六周作业总结

    作业06-接口.内部类 1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 2. 书面作业 1. c ...

  9. 如何用纯 CSS 创作一个同心圆弧旋转 loader 特效

    效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/OZmXQX 可交互视频教程 此视 ...

  10. git/github初级使用

    1.常见的github 国内最流行的php开发框架(thinkphp):https://github.com/top-think/thinkphp 全球最流行的php框架(laravel):https ...