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

来源:互联网 发布:windows更新后黑屏 编辑:程序博客网 时间:2024/06/05 03:31

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;

 

publicinterface IHelloWorld

{

   publicvoid sayHelloWorld();

  

   publicString 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文件。

<?xmlversion="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">

 

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

 

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

  

      <!-- 调用service -->

      <propertyname="service"ref="helloWorld"/>

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

      <propertyname="serviceName"value="hello"/>

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

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

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

      <propertyname="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文件。

<?xmlversion="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">

 

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

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

      <propertyname="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!

0 0
原创粉丝点击