a. IHelloWorld接口 sayHi() 方法
b. HelloWorld实现类 sayHi() 方法
c. applicationcontext配置
<bean id="helloWorld" class="com.tom.rmi.HelloWorld"> </bean> <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <!-- RMI服务名 --> <property name="serviceName" value="HelloWorld"/> <!-- Service实现类 --> <property name="service" ref="helloWorld"/> <!-- Service实现接口 --> <property name="serviceInterface" value="com.tom.rmi.IHelloWorld"/> <!-- RMI注册端口号 --> <property name="registryPort" value="9478" /> <!-- 服务通信端口,缺省为0 --> <property name="servicePort" value="9477" /> </bean>
2. 客户端
1.拷IHelloWorld接口到客户端项目(或者Class文件)
2.在mian方法中调用 通过获得HelloWorld 的bean调用sayHi()方法
获得HelloWorld方法方式
1.Spring context文件中配置
<bean id="serviceClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceInterface"> <value>com.tom.rmic.IHelloWorld</value> </property> <!-- serviceUrl以rmi开头,定义服务器地址与端口和服务名 --> <property name="serviceUrl"> <value>rmi://localhost:9478/HelloWorld</value> </property> </bean>
2.通过代码完成
public static Object getRemoteService() {
String serviceUrl = "rmi://" +"localhost"+ ":" +"9478" + "/" +"HelloWorld";
RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
rmiProxyFactoryBean.setServiceInterface(IHelloWorld.class);
rmiProxyFactoryBean.setServiceUrl(serviceUrl);
rmiProxyFactoryBean.setLookupStubOnStartup(false);
rmiProxyFactoryBean.setRefreshStubOnConnectFailure(true);
rmiProxyFactoryBean.afterPropertiesSet();
return rmiProxyFactoryBean.getObject();
}
然后获得HelloWorld对应的bean 调用sayHi()方法