---
title: "Spring-remoting使用心得1－RMI"
author: "javaocean"
date: "2009-09-22T10:39:51Z"
tags: []
slug: "spring-remoting-1-rmi"
draft: false
lastmod: "2009-09-22T10:39:51Z"
id: 474053
url: "https://javaocean.iteye.com/blog/474053"
---

看了《J2EE without EJB》的remote章节，忍不住写点代码试试，看看Spring的实现到底多巧妙。

1.先测试RMI服务的发布，测试代码如下：

java 代码

01. //MyService.java: remote interface for RMI
02. package test.spring.remote.rmi;
03. public interface MyService extends java.rmi.Remot {
04. public void doSomething() throws java.rmi.RemoteException;
05. }
06. //MyBusinessInterface.java: My own business interface, must has the same methods as MyService
07. package test.spring.remote.rmi;
08. public interface MyBusinessInterface {
09. public void doSomething();
10. }
11. //MyServiceImpl.java: the service implement class
12. package test.spring.remote.rmi;
13. public class MyServiceImpl implements MyService, MyBusinessInterface {
14. public void doSomething() {
15. System.out.println("MyServiceImpl.doSomething()");
16. }
17. }

 

Spring的context配置文件如下：

xml 代码

01. //spring-remote.xml
02. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
03. &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
04. "http://www.springframework.org/dtd/spring-beans.dtd"&gt;
05. &lt;beans&gt;
06. &lt;bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" /&gt;
07. &lt;bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"&gt;
08. &lt;property name="serviceName"&gt;
09. &lt;value&gt;myService&lt;/value&gt;
10. &lt;/property&gt;
11. &lt;property name="service"&gt;
12. &lt;ref bean="myService" /&gt;
13. &lt;/property&gt;
14. &lt;property name="serviceInterface"&gt;
15. &lt;value&gt;test.spring.remote.rmi.MyService&lt;/value&gt;
16. &lt;/property&gt;
17. &lt;property name="registryPort"&gt;
18. &lt;value&gt;1199&lt;/value&gt;
19. &lt;/property&gt;
20. &lt;/bean&gt;
21. &lt;/beans&gt;

再写一个测试程序，如下：

java 代码

1. //TestSpringRmi.java
2. package test.spring.remote.rmi;
3. import org.springframework.context.ApplicationContext;
4. import org.springframework.context.support.ClassPathXmlApplicationContext;
5. public class TestSpringRmi {
6. public static void main(String\[] args) {
7. ApplicationContext context = new ClassPathXmlApplicationContext("spring-remote.xml");
8. }
9. }

运行TestSpringRmi，报错如下：

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceExporter' defined in class path resource \[spring-remote.xml]: Initialization of bean failed; nested exception is java.rmi.StubNotFoundException: Stub class not found: test.spring.remote.rmi.MyServiceImpl\_Stub; nested exception is:  
 java.lang.ClassNotFoundException: test.spring.remote.rmi.MyServiceImpl\_Stub  
java.rmi.StubNotFoundException: Stub class not found: test.spring.remote.rmi.MyServiceImpl\_Stub; nested exception is:  
 java.lang.ClassNotFoundException: test.spring.remote.rmi.MyServiceImpl\_Stub

咦？Spring不是号称不需要自己生成stub么？怎么会出现“Stub class not found”呢？  
祭出google，从spring官方论坛搜到一个帖子：[http://forum.springframework.org/showthread.php?t=19185](http://forum.springframework.org/showthread.php?t=19185)，里面有条回复是：

I found the answer:  
The class org.springframework.remoting.rmi.RmiInvocationWrap per\_Stub is present in spring.jar, but not in the source tree as a Java file. Since I was running against the compiled Spring Java files, rather than the jar, it did not find it.

晕倒，Spring不会这么弱智吧，难道我以后使用的时候还得把jar包解压到class目录下？  
不甘心，再搜，找到这个帖子：[http://forum.springframework.org/showthread.php?t=12685](http://forum.springframework.org/showthread.php?t=12685)

在Juergen Hoeller的回复提示下，我再去看了jpetstore的配置文件，原来用以发布rmi的接口应该是pojo形式的MyBusinessInterface，而不是那个继承自Remote的MyService，修改自己的context配置文件：

xml 代码

01. //spring-remote.xml
02. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
03. &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
04. "http://www.springframework.org/dtd/spring-beans.dtd"&gt;
05. &lt;beans&gt;
06. &lt;bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" /&gt;
07. &lt;bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"&gt;
08. &lt;property name="serviceName"&gt;
09. &lt;value&gt;myService&lt;/value&gt;
10. &lt;/property&gt;
11. &lt;property name="service"&gt;
12. &lt;ref bean="myService" /&gt;
13. &lt;/property&gt;
14. &lt;property name="serviceInterface"&gt;
15. &lt;!-- &lt;value&gt;test.spring.remote.rmi.MyService&lt;/value&gt; --&gt;
16. &lt;value&gt;test.spring.remote.rmi.MyBusinessInterface&lt;/value&gt;
17. &lt;/property&gt;
18. &lt;property name="registryPort"&gt;
19. &lt;value&gt;1199&lt;/value&gt;
20. &lt;/property&gt;
21. &lt;/bean&gt;
22. &lt;/beans&gt;

再运行TestSpringRmi，成功了。console打印：  
03-02 14:51:56 INFO  \[RmiServiceExporter.java:236] Binding RMI service 'myService' to registry at port '1199'

2.再继续测试客户端调用，先修改context配置如下：

xml 代码

01. //spring-remote.xml
02. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
03. &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
04. "http://www.springframework.org/dtd/spring-beans.dtd"&gt;
05. &lt;beans&gt;
06. &lt;bean id="rmiService"
07. class="org.springframework.remoting.rmi.RmiProxyFactoryBean"&gt;
08. &lt;property name="serviceInterface"&gt;
09. &lt;value&gt;test.spring.remote.rmi.MyBusinessInterface&lt;/value&gt;
10. &lt;/property&gt;
11. &lt;property name="serviceUrl"&gt;
12. &lt;value&gt;rmi://localhost:1199/myService&lt;/value&gt;
13. &lt;/property&gt;
14. &lt;/bean&gt;
15. &lt;bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" /&gt;
16. &lt;bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"&gt;
17. &lt;property name="serviceName"&gt;
18. &lt;value&gt;myService&lt;/value&gt;
19. &lt;/property&gt;
20. &lt;property name="service"&gt;
21. &lt;ref bean="myService" /&gt;
22. &lt;/property&gt;
23. &lt;property name="serviceInterface"&gt;
24. &lt;!-- &lt;value&gt;test.spring.remote.rmi.MyService&lt;/value&gt; --&gt;
25. &lt;value&gt;test.spring.remote.rmi.MyBusinessInterface&lt;/value&gt;
26. &lt;/property&gt;
27. &lt;property name="registryPort"&gt;
28. &lt;value&gt;1199&lt;/value&gt;
29. &lt;/property&gt;
30. &lt;/bean&gt;
31. &lt;/beans&gt;

再修改测试代码，添加客户端调用：

java 代码

01. //TestSpringRmi.java
02. package test.spring.remote.rmi;
03. import org.springframework.context.ApplicationContext;
04. import org.springframework.context.support.ClassPathXmlApplicationContext;
05. public class TestSpringRmi {
06. public static void main(String\[] args) {
07. ApplicationContext context = new ClassPathXmlApplicationContext("spring-remote.xml");
08. MyBusinessInterface service = (MyBusinessInterface) context.getBean("rmiService");
09. service.doSomething();
10. }
11. }

运行TestSpringRmi，报错如下：

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rmiService' defined in class path resource \[spring-remote.xml]: Initialization of bean failed; nested exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:  
 java.net.ConnectException: Connection refused: connect  
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:  
 java.net.ConnectException: Connection refused: connect

仔细检查，原来自己把生成rmi客户端的bean映射放到了发布rmi服务的serviceExporter之前了，调换一下顺序：

    **xml 代码**

01. //spring-remote.xml
02. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
03. &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
04. "http://www.springframework.org/dtd/spring-beans.dtd"&gt;
05. &lt;beans&gt;
06. &lt;bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" /&gt;
07. &lt;bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"&gt;
08. &lt;property name="serviceName"&gt;
09. &lt;value&gt;myService&lt;/value&gt;
10. &lt;/property&gt;
11. &lt;property name="service"&gt;
12. &lt;ref bean="myService" /&gt;
13. &lt;/property&gt;
14. &lt;property name="serviceInterface"&gt;
15. &lt;!-- &lt;value&gt;test.spring.remote.rmi.MyService&lt;/value&gt; --&gt;
16. &lt;value&gt;test.spring.remote.rmi.MyBusinessInterface&lt;/value&gt;
17. &lt;/property&gt;
18. &lt;property name="registryPort"&gt;
19. &lt;value&gt;1199&lt;/value&gt;
20. &lt;/property&gt;
21. &lt;/bean&gt;
22. &lt;bean id="rmiService"
23. class="org.springframework.remoting.rmi.RmiProxyFactoryBean"&gt;
24. &lt;property name="serviceInterface"&gt;
25. &lt;value&gt;test.spring.remote.rmi.MyBusinessInterface&lt;/value&gt;
26. &lt;/property&gt;
27. &lt;property name="serviceUrl"&gt;
28. &lt;value&gt;rmi://localhost:1199/myService&lt;/value&gt;
29. &lt;/property&gt;
30. &lt;/bean&gt;
31. &lt;/beans&gt;

运行TestSpringRmi，结果如下：  
03-02 15:01:24 INFO  \[RmiServiceExporter.java:236] Binding RMI service 'myService' to registry at port '1199'  
03-02 15:01:24 INFO  \[RmiClientInterceptor.java:128] RMI stub \[rmi://localhost:1199/myService] is an RMI invoker  
MyServiceImpl.doSomething()

经过一番浅尝辄止，初步得出几个结论：  
1.Spring对RMI的支持果然很不错，在Cglib等工具的支持下，使用RMI终于可以同Naming、rmic和stub告别了。  
2.用以发布RMI的接口不能从java.rmi.Remote继承而来，否则就会出现“Stub class not found”的错误，原因有待深究。  
3.Spring的BeanFactory创建bean实例是有序的，向RMI、JNDI、WebService等注册服务性质的应用，同一应用中的客户端要根据其依赖性调整配置顺序。