服务端:

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. 语音行业技术领先者Nuance诚招ASR/NLP研发工程师和软件工程师

    Nuance is a leading provider of voice and language solutions for businesses and consumers around the ...

  2. 初次改app

    没有学过安卓,突然需要改app里的一个bug,一个没搞过安卓的人要怎么入手去改这个安卓的代码呢?下面看看我入手的步骤 首先,页面上有几个文字“曲线分析”,那么在项目里找到这个文字,就可以找到这个页面的 ...

  3. QT_6_QMainWindow

    QMainWindow 1.1. 菜单栏 1.1.1. 只有一个 1.1.2. QMenuBar *bar = MenuBar(); 1.1.3. 设置到窗口中 setMenuBar(bar); 1. ...

  4. Navicat 模型生成表

    打开模型 -> 左上角文件 -> 导出SQL 打开sql文件,将sql在数据库执行,注意主键递增.日期类型 根据当前时间戳更新是否需要(默认选中的)等等

  5. SQL Prompt 格式化SQL会自动插入分号的问题

    一.问题 安装新版SQL Prompt,格式化SQL都会自动在SQL末端插入分号 格式化前 格式化后 二.解决方法 选择SQL Prompt下的Options... 选择左侧的Format下Style ...

  6. 高德地图api之location定位

    关于定位,分为GPS定位和网络定位.本文将详细描述的浏览器定位,属于网络定位.这是一种通过使用高德JS-API来实现位置定位.城市定位的方法,包含了IP定位,检索等多种网络定位方式.如果您的手机支持G ...

  7. Swift 中的Range和NSRange不同

    Swift中的Ranges和Objective-C中的NSRange有很大的不同,我发现在处理Swift中Ranges相关的问题的时候,总是要花费比我想象的更多的时间.不过,现在回过头来看看,发现Sw ...

  8. vue2.0中transition组件的用法

    作用:实现元素进入/离开的过渡效果. 首先,让我们举个栗子: <!DOCTYPE html> <html lang="en"> <head> & ...

  9. OpenJudge-百练-2755

    这道题用递归写的话还是很好写的,我们设递归函数的名称为Ways(w,k) . 它的含义就是,w的大小,取k个物品,有多少种方式. 我们可以知道递归的边界条件就是当w的大小为0的时候,我们的方法数只有一 ...

  10. 02-Mysql中的运算符

    Mysql中运算符 1.算术运算符运算符 作用+   加法-    减法*    乘法/,DIV     除法,返回商%,MOD       除法,返回余数 mysql root@localhost: ...