Spring与RMI调用

一:服务器端
1. 要暴露的服务的接口:

java 代码
  1. package com.excellence.webservice;   
  2.   
  3. import java.util.List;   
  4.   
  5. public interface AccountService {   
  6. public void insertAccount(Account account);   
  7. public List getAccounts(String name);   
  8. }   

2. 实现了该接口的类:

java 代码
  1. package com.excellence.webservice;   
  2.   
  3. import java.util.List;   
  4.   
  5. public class AccountServiceImpl implements AccountService {   
  6.   
  7. public void insertAccount(Account account) {   
  8. System.out.println("inser!");   
  9.   
  10. }   
  11.   
  12. public List getAccounts(String name) {   
  13. System.out.println("get");   
  14. return null;   
  15. }   
  16.   
  17. }   

3. 在配置文件中公布改接口为RMI

xml 代码
  1. <bean id="accountService" class="com.excellence.webservice.AccountServiceImpl" />  
  2. <bean name="service" class="org.springframework.remoting.rmi.RmiServiceExporter">  
  3. <property name="serviceName" value="AccountService" ></property>  
  4. <property name="service" ref="accountService"></property>  
  5. <property name="serviceInterface" value="com.excellence.webservice.AccountService"></property>  
  6. <property name="registryPort" value="1199"></property>  
  7. </bean>  

4. 运行该RMI:

java 代码
  1. public class Demo {   
  2.   
  3.   
  4. public static void main(String[] args) {   
  5. ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:applicationCOntext.xml");   
  6. RmiServiceExporter obj = (RmiServiceExporter)ctx.getBean("service");   
  7.   
  8. }   
  9.   
  10. }   

二、客户端
1.拿到该服务的接口:

java 代码
  1. package com.excellence.webservice;   
  2.   
  3. import java.util.List;   
  4.   
  5. public interface AccountService {   
  6. public void insertAccount(Account account);   
  7. public List getAccounts(String name);   
  8. }   

 

2.在配置文件中进行配置:

xml 代码
  1. <bean id="accClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
  2. <property name="serviceUrl" value="rmi://localhost:1199/AccountService"></property>  
  3. <property name="serviceInterface" value="com.excellence.webservice.AccountService"></property>  
  4. </bean>  

3.调用RMI的方法:

java 代码
  1. public static void main(String[] args) {   
  2. ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");   
  3. AccountService service = (AccountService)ctx.getBean("accClient");   
  4. service.insertAccount(new Account());   
  5. service.getAccounts("dd");   
  6.   
  7.   
  8. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值