服务端:

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. COGS 886. [USACO 4.2] 完美的牛栏

    ★★☆   输入文件:stall4.in   输出文件:stall4.out   简单对比时间限制:1 s   内存限制:128 MB USACO/stall4(译by Felicia Crazy) ...

  2. elasticsearch插入索引文档 对数字字符串的处理

    对于字符串在搜索匹配的时候,字符串是数字的话需要匹配的是精准匹配,如果是部分匹配字符串的话,需要进行处理,把数字型字符串作为一个字符中的数组表示插入之后显示如下: 如果插入之后显示如画线部分的话,则表 ...

  3. nginx的编译安装

    下载源码 wget http://nginx.org/download/nginx-1.15.9.tar.gz 安装开发包组 yum groupinstall "Development To ...

  4. CentOS7.6 静态IP配置

    1:选中安装好的虚拟机,点击“编辑”,然后选择“虚拟网络编辑器(N…)”,如下图所示: 2:选择桥接模式,在桥接到指定的本地网卡即可."确定"保存 3:选中虚拟机,右击虚拟机,选择 ...

  5. mysql时间的处理

    mysql中格式化时间为: 1,DATE_FORMAT(APPLYDATE,'%Y-%m-%d %H:%i:%S') AS APPLYDATE 2,DATE_FORMAT(CHKSIGNDATE, ' ...

  6. JS实现两版本号大小比较

    JavaScript实现版本号比对(含字母) 昨天,有一道面试题,要求是这样的: 用你熟悉的编程语言,实现一个比较任意两个软件版本号大小的函数,如1.2.3a与1.2.4b进行比较,后者版本号更大,要 ...

  7. luogu P1364 医院设置

    题目描述 设有一棵二叉树,如图: 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接点之间的距离为1.如上 ...

  8. poj-1163 动态规划

    这道题目并不能直接使用递归,因为 7(1) 7(1)         7(1) 7(1)      7(2)         7(1) 7(1)       7(3)         7(3)    ...

  9. Oracle数据库日常SQL的使用

    DDL 语句(数据定义语言Create.Alter. Drop.Truncate) 1.建表:create table 表名(): 2.复制表结构及其数据:create table 新表名 as se ...

  10. 【BZOJ 2462】矩阵模板 (二维哈希)

    题目 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在 原矩阵中出现过. 所谓01矩阵,就是矩阵中所有元素不是0就是1. 输入 输入文件的第一行为M.N.A.B,参见 ...