spring RMI

本文介绍如何利用Spring 3.0框架配置RMI(远程方法调用),通过示例展示了服务端与客户端的搭建过程。包括XML配置文件、接口定义及其实现等内容。

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

Rmiremote method invocation,即远程方法调用),使用rmi,使用远程的方法就像使用本地的方法一样方便。

基于spring3.0

1.首先在src目录下建立两个xml文件

 applicationContext_rmi_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-3.0.xsd">
 <bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  <property name="serviceUrl" value="rmi://localhost:8088/hello" />
  <property name="serviceInterface" value="lavasoft.sturmi.HelloService" />
 </bean>
 <bean id="helloServiceClient" class="lavasoft.sturmi.HelloClient">
  <property name="helloService" ref="helloService" />
 </bean>
</beans>

 

 applicationContext_rmi_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:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 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/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="helloService" class="lavasoft.sturmi.HelloServiceImpl"/>
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="helloService"/>
<!-- 定义服务名 -->
<property name="serviceName" value="hello"/>

//暴露给客户端的接口
<property name="serviceInterface" value="lavasoft.sturmi.HelloService"/>
<property name="registryPort" value="8088"/>
</bean>
</beans>

 

2.写一个接口(服务端)//客户端也要提供相同的接口与实现

package lavasoft.sturmi;

public interface HelloService {
 public String helloWorld();
 public String sayHelloToSomeBody(String someBodyName);

}

 

3.写一个接口的实现

 

package lavasoft.sturmi;

public class HelloServiceImpl implements HelloService {
 public HelloServiceImpl() {}

 @Override
 public String helloWorld() {
  // TODO Auto-generated method stub
  return "helloWorld";
 }

 @Override
 public String sayHelloToSomeBody(String someBodyName) {
  // TODO Auto-generated method stub
  return "你好," + someBodyName + "!";

 }


}

客户端实现的代码:

package lavasoft.sturmi;

import java.rmi.RemoteException;

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

public class HelloClient {
 private HelloService helloService;

 public static void main(String[] args) throws RemoteException {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext_rmi_client.xml");
  HelloService hs = (HelloService) ctx.getBean("helloService");
  System.out.println(hs.helloWorld());
  System.out.println(hs.sayHelloToSomeBody("Lavasoft"));
}

 public HelloService getHelloService() {
  return helloService;
 }

 public void setHelloService(HelloService helloService){
  this.helloService = helloService;
 }

 
}

服务端实现的代码:

package lavasoft.sturmi;

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

public class HelloHost {
 public static void main(String[] args) {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext_rmi_server.xml");
  System.out.println("RMI服务伴随Spring的启动而启动了.....");
}

}

 

http://blog.sina.com.cn/s/blog_616e189f0100nkxn.html
 

同时还可以参考:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值