手机版
你好,游客 登录 注册
背景:
阅读新闻

Spring与RMI集成实现远程访问

[日期:2011-10-06] 来源:Linux社区  作者:shirdrn [字体:大 中 小]

客户端调用服务

客户端配置client.xml如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  5.   
  6.     <bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
  7.         <property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />  
  8.         <property name="serviceInterface"  
  9.             value="org.shirdrn.spring.remote.rmi.AccountService" />  
  10.     </bean>  
  11.   
  12. </beans>  

配置中,将一个serviceUrl和serviceInterface注入给RmiProxyFactoryBean,即可进行远程方法调用。调用示例如下所示:

  1. package org.shirdrn.spring.remote.rmi;  
  2.   
  3. import org.apache.log4j.Logger;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class RmiClient {  
  8.   
  9.     private static final Logger LOG = Logger.getLogger(RmiClient.class);  
  10.       
  11.     public static void main(String[] args) {  
  12.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  13.                 "org/shirdrn/spring/remote/rmi/client.xml");  
  14.         AccountService accountService = (AccountService) ctx  
  15.                 .getBean("mobileAccountService");  
  16.         String result = accountService.shoopingPayment("13800138000", (byte) 5);  
  17.         LOG.info(result);  
  18.     }  
  19.   
  20. }  

可见,实现远程访问变得非常容易。

linux
相关资讯       rmi  Spring 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款