(1) define the interface: Java code
- Package com. logcd. Spring. RMI;
- Public interface helloservice {
- Public String dohello (string name );
- }
Package com. logcd. Spring. RMI; public interface helloservice {Public String dohello (string name );}
(2) interface implementation: Java code
- Package com. logcd. Spring. RMI;
- Public class helloserviceimpl implements helloservice {
- Public String dohello (string name ){
- Return "hello," + name;
- }
- }
Package com. logcd. Spring. RMI; public class helloserviceimpl implements helloservice {Public String dohello (string name) {return "hello," + name ;}}
(3) rmi-server.xml Java code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Doctype beans public "-// spring/DTD bean/EN"
- Http://www.springframework.org/dtd/spring-beans.dtd>
- <Beans>
- <Bean id = "helloservice" class = "com. logcd. Spring. RMI. helloserviceimpl"/>
- <! -- Rmiserviceexporter displays support for exposing any non-RMI service using the RMI caller -->
- <Bean id = "serviceexporter"
- Class = "org. springframework. remoting. RMI. rmiserviceexporter">
- <Property name = "service" ref = "helloservice"/>
- <Property name = "serviceinterface"
- Value = "com. logcd. Spring. RMI. helloservice"/>
- <! -- Defines that the name of the service to be exposed can be different from that of the output bean. The client uses this name to call the service. -->
- <Property name = "servicename" value = "helloservice"/>
- <! -- Overwrite the RMI registration port number (1099). Generally, the application server also maintains RMI registration, so it is best not to conflict. -->
- <Property name = "registryport" value = "1199" type = "regxph" text = "yourobjectname"/>
- </Bean>
- </Beans>
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype beans public "-// spring/DTD bean/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id = "helloservice" class = "com. logcd. spring. RMI. helloserviceimpl "/> <! -- Rmiserviceexporter displays support for exposing any non-RMI service using the RMI caller --> <bean id = "serviceexporter" class = "org. springframework. remoting. RMI. rmiserviceexporter "> <property name =" service "ref =" helloservice "/> <property name =" serviceinterface "value =" com. logcd. spring. RMI. helloservice "/> <! -- Defines the name of the service to be exposed and the name of the output bean. The client calls the service through this name --> <property name = "servicename" value = "helloservice"/> <! -- Override the RMI registration port number (1099). Generally, the application server also maintains RMI registration, it is best not to conflict --> <property name = "registryport" value = "1199"/> </bean> </beans>
(4) rmi-client.xml Java code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Doctype beans public "-// spring/DTD bean/EN"
- Http://www.springframework.org/dtd/spring-beans.dtd>
- <Beans>
- <! -- Use rmiproxyfactorybean to connect to the server -->
- <Bean id = "serviceproxy"
- Class = "org. springframework. remoting. RMI. rmiproxyfactorybean">
- <Property name = "serviceurl"
- Value = "RMI: // localhost: 1199/helloservice"/>
- <Property name = "serviceinterface"
- Value = "com. logcd. Spring. RMI. helloservice"/>
- </Bean>
- </Beans>
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype beans public "-// spring/DTD bean/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <! -- Use rmiproxyfactorybean to connect to the server --> <bean id = "serviceproxy" class = "org. springframework. remoting. RMI. rmiproxyfactorybean "> <property name =" serviceurl "value =" RMI: // localhost: 1199/helloservice "/> <property name =" serviceinterface "value =" com. logcd. spring. RMI. helloservice "/> </bean> </beans>
(5) test Java code
- Package com. logcd. Spring. RMI;
- Import org. springframework. Context. applicationcontext;
- Import org. springframework. Context. Support. classpathxmlapplicationcontext;
- Public class testspringrmi {
- Public static void main (string ARGs []) {
- Applicationcontext context = new classpathxmlapplicationcontext (
- New String [] {"rmi-server.xml", "rmi-client.xml "});
- Helloservice service = (helloservice) Context. getbean ("serviceproxy ");
- System. Out. println (service. dohello ("logcd "));
- }
- }
Package COM. logcd. spring. RMI; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; public class testspringrmi {public static void main (string ARGs []) {applicationcontext context = new classpathxmlapplicationcontext (New String [] {"rmi-server.xml", "rmi-client.xml "}); helloservice service = (helloservice) context. getbean ("serviceproxy"); system. out. println (service. dohello ("logcd "));}}
From: http://log-cd.iteye.com/blog/213908