/**
* 定义一个远程接口
*
* @author leizhimin 2009-8-17 13:53:38
*/
public interface HelloService {
/**
* 简单的返回“Hello World!"字样
*
* @return 返回“Hello World!"字样
*/
public String helloWorld();
/**
* 一个简单的业务方法,根据传入的人名返回相应的问候语
*
* @param someBodyName 人名
* @return 返回相应的问候语
*/
public String sayHelloToSomeBody(String someBodyName);
}
/**
* 远程的接口的实现
*
* @author leizhimin 2009-8-17 13:54:38
*/
public class HelloServiceImpl implements HelloService {
public HelloServiceImpl() {
}
/**
* 简单的返回“Hello World!"字样
*
* @return 返回“Hello World!"字样
*/
public String helloWorld() {
return "Hello World!";
}
/**
* 一个简单的业务方法,根据传入的人名返回相应的问候语
*
* @param someBodyName 人名
* @return 返回相应的问候语
*/
public String sayHelloToSomeBody(String someBodyName) {
return "你好," + someBodyName + "!";
}
}
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<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>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 通过Spring发布RMI服务
*
* @author leizhimin 2009-8-17 14:22:06
*/
public class HelloHost {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext_rmi_server.xml");
System.out.println("RMI服务伴随Spring的启动而启动了.....");
}
}
<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="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://192.168.14.117:8088/hello"/>
<property name="serviceInterface" value="lavasoft.sturmi.HelloService"/>
</bean>
<bean id="helloServiceClient" class="lavasoft.sturmi.HelloClient">
<property name="helloService" ref="helloService"/>
</bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.rmi.RemoteException;
/**
* 通过Spring来调用RMI服务
*
* @author leizhimin 2009-8-17 14:12:46
*/
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 void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
}