JAVA RMI 学习二

     刚刚学习了RMI基础运用,现在来看一下 spring对RMI的支持。

  这边要注意一点,和普通的RMI相比,和spring结合不再继承 UnicastRemoteObject

接口变成

public class StudentMsg  implements IStudentMsg

 而不是之前的

public class StudentMsg extends UnicastRemoteObject implements IStudentMsg

 

 

其他的配置不变,现在看看spring配置文件

服务端:applicationContext.xml

<!--服务端--> 
	<bean id="studentServiceImpl" class="com.Rmi.Serv.StudentMsg" scope="prototype"/>
	<!-- 将类为一个RMI服务 -->
	<bean id="studentService" class="org.springframework.remoting.rmi.RmiServiceExporter">
		<!-- 服务类 -->
		<property name="service" ref="studentServiceImpl" />
		<!-- 服务名 -->
		<property name="serviceName" value="studentServices" />
		<!-- 服务接口 -->
		<property name="serviceInterface" value="com.Rmi.Imp.IStudentMsg" />
		<!-- 服务端口 -->
		<property name="registryPort" value="9999" />
	</bean>

 

 

 客户端:applicationContext2.xml

    <!--客户端--> 
    <bean id="myClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 
        <property name="serviceUrl" value="rmi://127.0.0.1:9999/studentServices"/> 
        <property name="serviceInterface" value="com.Rmi.Imp.IStudentMsg"/> 
    </bean> 

 

 

    就是那么简单。

 

 

   测试一下:

客户端:
public class ClientTest {
public static void main(String[] args) throws RemoteException {
  plicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
  tudentMsg hms = context.getBean("myClient", IStudentMsg.class);
  System.out.println(hms.findStudentArray().length);
}

   

public class ServerTest {
 public static void main(String[] args) {
  //初始化工作只能运行一次;运行多次的话,会启动多个服务
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 }
}
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值