客户端调用服务
客户端配置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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
- <property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />
- <property name="serviceInterface"
- value="org.shirdrn.spring.remote.rmi.AccountService" />
- </bean>
- </beans>
配置中,将一个serviceUrl和serviceInterface注入给RmiProxyFactoryBean,即可进行远程方法调用。调用示例如下所示:
- package org.shirdrn.spring.remote.rmi;
- import org.apache.log4j.Logger;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class RmiClient {
- private static final Logger LOG = Logger.getLogger(RmiClient.class);
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "org/shirdrn/spring/remote/rmi/client.xml");
- AccountService accountService = (AccountService) ctx
- .getBean("mobileAccountService");
- String result = accountService.shoopingPayment("13800138000", (byte) 5);
- LOG.info(result);
- }
- }
可见,实现远程访问变得非常容易。
