Spring RMI服务

1.Spring中配置 RMI服务 服务端 发布

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()方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值