RMI学习笔记(2)-Spring集成RMI

本文介绍如何利用Spring框架简化RMI应用开发,通过整合Spring与RMI,减少底层编程复杂度,实现服务端接口透明化,增强业务关注点。详细步骤包括创建项目结构、接口定义、服务实现、配置文件编写以及客户端调用,最终通过实例展示从服务端到客户端的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RMI学习笔记(2)——Spring集成RMI

Spring集成RMI使得RMI应用更加简单,对RMI底层的编程变得透明化,使我们更少的关注代码,更多的关注业务。

使用Spring集成RMI之后,我们在服务端不在需要编写复杂的逻辑代码,接口也不用去继承Remote类,不需要抛出复杂的异常,就是普通的接口而已,而且接口的实现也是普通的实现。下面我们来看具体的实例:

1.      创建三个工程spring_rmi_interface、spring_rmi_server、spring_rmi_client

说明:spring_rmi_interface:用于编写公用接口

          spring_rmi_server:服务端项目

          spring_rmi_client:客户端项目

       2.在spring_rmi_interface中创建一个接口类IHelloWorld.java

       package com.spring.rmi.interfaces;

 

public interface IHelloWorld

{

   public void sayHelloWorld();

  

   public String sayHelloJava(Stringname);

}

3.将编写好的接口项目打包成jar文件,分别加入spring_rmi_server、spring_rmi_client项目中,分别给这两个项目添加spring支持。

4.实现服务端项目,在spring_rmi_server项目中创建一个类HelloWorldImpl.java,实现IHelloWorld接口,HelloWorldImpl.java

       package com.spring.rmi.impl;

 

import com.spring.rmi.interfaces.IHelloWorld;

 

public class HelloWorldImplimplementsIHelloWorld

{

 

   @Override

   public String sayHelloJava(Stringarg0)

   {

      return arg0 + "说:HelloJava!";

   }

 

   @Override

   public void sayHelloWorld()

   {

      System.out.println("HelloWorld!");

   }

 

}

       5.配置server.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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

 

   <bean id="helloWorld" class="com.spring.rmi.impl.HelloWorldImpl"/>

 

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

  

      <!-- 调用service -->

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

      <!-- value值是提供给客户端调用的 -->

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

      <!-- 创建service接口 -->

      <property name="serviceInterface"value="com.spring.rmi.interfaces.IHelloWorld"/>

      <!-- 注册RMI服务端口 -->

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

   </bean>

 

</beans>

6.编写服务端启动类ServerStart.java

package com.spring.rmi.serverstart;

 

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public classServerStart

{

 

   public static void main(String[] args)

   {

      new ClassPathXmlApplicationContext("server.xml");

      System.out.println("RMI服务端已经启动");

   }

}

       7.在spring_rmi_client项目中配置client.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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

 

   <bean id="helloworld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

      <property name="serviceUrl"value="rmi://localhost:8888/hello"/>

      <property name="serviceInterface"value="com.spring.rmi.interfaces.IHelloWorld"/>

   </bean>

</beans>

8.实现客户端调用,创建一个客户端调用类ClientStart.java

package com.spring.rmi.client;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.spring.rmi.interfaces.IHelloWorld;

 

public classClientStart

{

   public static void main(String[] args)

   {

      ApplicationContextctx = newClassPathXmlApplicationContext("client.xml");

      IHelloWorldhelloWorld = (IHelloWorld) ctx.getBean("helloworld");

      helloWorld.sayHelloWorld();

      System.out.println(helloWorld.sayHelloJava("小明"));

   }

}

9.启动测试:

启动服务端主程序ServerStart.java

       Console:RMI服务端已经启动

HelloWorld!

启动客户端调用程序ClientStart.java

Console:小明说:HelloJava!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值