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

扫描二维码关注公众号,回复: 623398 查看本文章
    <!--客户端--> 
    <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");
 }
}
 

猜你喜欢

转载自hash.iteye.com/blog/1840583