一:服务器端
1. 要暴露的服务的接口:
java 代码
- package com.excellence.webservice;
-
- import java.util.List;
-
- public interface AccountService {
- public void insertAccount(Account account);
- public List getAccounts(String name);
- }
2. 实现了该接口的类:
java 代码
- package com.excellence.webservice;
-
- import java.util.List;
-
- public class AccountServiceImpl implements AccountService {
-
- public void insertAccount(Account account) {
- System.out.println("inser!");
-
- }
-
- public List getAccounts(String name) {
- System.out.println("get");
- return null;
- }
-
- }
3. 在配置文件中公布改接口为RMI
xml 代码
- <bean id="accountService" class="com.excellence.webservice.AccountServiceImpl" />
- <bean name="service" class="org.springframework.remoting.rmi.RmiServiceExporter">
- <property name="serviceName" value="AccountService" ></property>
- <property name="service" ref="accountService"></property>
- <property name="serviceInterface" value="com.excellence.webservice.AccountService"></property>
- <property name="registryPort" value="1199"></property>
- </bean>
4. 运行该RMI:
java 代码
- public class Demo {
-
-
- public static void main(String[] args) {
- ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:applicationCOntext.xml");
- RmiServiceExporter obj = (RmiServiceExporter)ctx.getBean("service");
-
- }
-
- }
二、客户端
1.拿到该服务的接口:
java 代码
- package com.excellence.webservice;
-
- import java.util.List;
-
- public interface AccountService {
- public void insertAccount(Account account);
- public List getAccounts(String name);
- }
2.在配置文件中进行配置:
xml 代码
- <bean id="accClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
- <property name="serviceUrl" value="rmi://localhost:1199/AccountService"></property>
- <property name="serviceInterface" value="com.excellence.webservice.AccountService"></property>
- </bean>
3.调用RMI的方法:
java 代码
- public static void main(String[] args) {
- ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
- AccountService service = (AccountService)ctx.getBean("accClient");
- service.insertAccount(new Account());
- service.getAccounts("dd");
-
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|