《pro Spring》学习笔记之Spring集成RMI实例

本文介绍如何使用Spring框架简化RMI开发过程。通过Spring提供的RmiServiceExporter和RmiProxyFactoryBean,可以轻松地将任意POJO转变为RMI服务,并在客户端调用这些服务。示例展示了服务接口定义、服务实现、服务端与客户端配置。
部署运行你感兴趣的模型镜像

有关rmi的相关知识,本文并不涉及,只关心Spring如何简化RMI的开发

Spring利用代理,完成RMI服务端和客户端的开发,可以使任意POJO的方法进行暴露

服务接口:

 

package ch16.SimpleRMI;

public interface HelloWorld {
   
public String getMessage();
}

 

服务实现:

 

package ch16.SimpleRMI;

public class HelloWorldImpl implements HelloWorld {

    
public String getMessage() {
        
        
return "hello world";
    }


}

 

服务端配置文件:applicationContext.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"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorldService" class="org.springframework.remoting.rmi.RmiServiceExporter">
  
<property name="serviceName">
    
<value>HelloWorld</value>
  
</property>
  
<property name="service">
    
<bean class="ch16.SimpleRMI.HelloWorldImpl"/>
  
</property>
  
<property name="serviceInterface">
    
<value>ch16.SimpleRMI.HelloWorld</value>
  
</property>
  
<property name="registryPort">
    
<value>9000</value>
  
</property>
  
<property name="servicePort">
    
<value>9001</value>
  
</property>
  
</bean>
</beans>

 

客户端配置文件:applicationContext-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"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloworldService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
 
<property name="serviceUrl">
   
<value>rmi://localhost:9000/HelloWorld</value>
 
</property>
 
<property name="serviceInterface">
    
<bean class="ch16.SimpleRMI.HelloWorld"></bean>
  
</property>
</bean>

<bean id="helloworldClient" class="ch16.SimpleRMI.HelloWorldClient">
  
<property name="helloworldService">
    
<ref local="helloworldService"/>
  
</property>
</bean>
</beans>

 

服务端测试代码:

运行后并没有立即退出,因为远程服务是运行在后台的,一直等待客户端的链接

 

package ch16.SimpleRMI;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        ApplicationContext context 
= new ClassPathXmlApplicationContext("ch16/SimpleRMI/applicationContext.xml");
        System.out.println(
"host stated...");
    }


}

 

客户端:

 

package ch16.SimpleRMI;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldClient {

    
private HelloWorld helloworldService;
    
public HelloWorld getHelloworldService() {
        
return helloworldService;
    }

    
public void setHelloworldService(HelloWorld helloworldService) {
        
this.helloworldService = helloworldService;
    }

    
public static void main(String[] args) {
        ApplicationContext context 
= new ClassPathXmlApplicationContext("ch6/SimpleRMI/applicationContext-client.xml");
        HelloWorldClient helloWorldClient
=(HelloWorldClient)context.getBean("helloworldClient");
        helloWorldClient.run();
    }

    
public void run(){
        System.out.println(helloworldService.getMessage());
    }


}

 

运行后,可以看到后台打印出了,服务端输出的Hello World

您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值