spring 中RMI的应用
利用Spring来实现RMI,不用实现remote接口,也不用调用rmic编译stub和skeleton,
服务端可以定义org.springframework.remoting.rmi.RmiServiceExporter类完成RMI服务器实现.
客户端只要定义org.springframework.remoting.rmi.RmiProxyFactoryBean,告知rmi的url和接口
服务器实现:
接口: IHello.java
?
?
package com.callan.Test;
public interface IHello {
?public String hello(String name);
}
?
?
实现类:HelloImp.java
?
package com.callan.Test;
public class HelloImp implements IHello{
?public String hello(String name){
??return "hello:" + name;
?}
}
?
?
?
?
服务端spring的配置:
?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
??? "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?<bean id="helloService" 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="serviceClient"
??class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
??
??<property name="serviceInterface">
???<value>com.callan.Test.IHello</value>
??</property>
??<!-- serviceUrl以rmi开头,定义服务器地址与端口和服务名 -->
??<property name="serviceUrl">
???<value>rmi://localhost:8888/hello</value>
??</property>
?</bean>
</beans>
?
客户端调用
package com.callan.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class RMIClient {
?public static void main(String[] args) {
??ApplicationContext content = new FileSystemXmlApplicationContext("E:/workspace/rmiClient/src/applicationContext.xml");
??
??IHello iHello = (IHello)content.getBean("serviceClient");
??
??System.out.println(iHello.hello("callan"));
?}
}