Spring的RMI远程调用 - (示例)

一、定义远程服务器上接口

public interface RMIService {

public String getInfo(); }

二、实现远程服务器上接口

public class RMIServiceImpl implements RMIService{

public String getInfo() { String msg = "RMI 服务!"; return msg; }

}

三、配置服务器文件 applicationContext_service.xml

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default- default-lazy-init="false"> <!-- 用户RMI接口实现类 --> <bean name="RMIServer" class="com.rim.test.RMIServiceImpl"/> <!-- 暴露RMI服务配置 --> <bean name="rimServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="RMIServer"/> <!-- 暴露给客户调用服务 --> <property name="serviceName" value="TestService"/> <!-- 调用接口 --> <property name="serviceInterface" value="com.rim.test.RMIService"/> <property name="registryPort" value="1099"/> </bean> </beans> 四、启动服务器

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ServiceMainTest {

public static void main(String[] args) { ApplicationContext appContext=new ClassPathXmlApplicationContext("applicationContext_service.xml"); appContext.getBean("rimServiceExporter");

} }

五、配置客户端文件 applicationContext_client.xml

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default- default-lazy-init="true"> <!-- 客户端调用RMI服务配置 --> <bean name="RMIServiceImpl" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://192.168.1.179:1099/TestService"/> <property name="serviceInterface" value="com.rim.test.RMIService"/> </bean> </beans>

六、客户端获取远程接口上的数据

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ClientMainTest {

public static void main(String[] args) { ApplicationContext appContext1=new ClassPathXmlApplicationContext("applicationContext_client.xml"); RMIService rmi = (RMIService)appContext1.getBean("RMIServiceImpl"); System.out.println(rmi.getInfo()); }

 

}

Spring的RMI远程调用 - (示例),布布扣,bubuko.com

Spring的RMI远程调用 - (示例)

上一篇:JavaScript 闭包及其机制


下一篇:Java 8 Lambda表达式探险