http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp
http://shouce.jb51.net/spring/remoting.html
http://www.javaworld.com/article/2072246/simple-spring-http-remoting-example.html
Required Libraries
aopalliance.jar
commons-logging.jar
log4j.jar
org.springframework.aop.jar
org.springframework.asm.jar
org.springframework.beans.jar
org.springframework.context.jar
org.springframework.context.support.jar
org.springframework.core.jar
org.springframework.expression.jar
Server 端:springremotingrmiserver
package com.studytrails.tutorials.springremotingrmiserver;
public interface GreetingService {
String getGreeting(String name);
}
package com.studytrails.tutorials.springremotingrmiserver;
public interface GreetingService {
String getGreeting(String name);
}
package com.studytrails.tutorials.springremotingrmiserver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StartRmiServer {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-server.xml");
System.out.println("Waiting for Request from Client ...");
}
}
spring-config-server.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="greetingService"
class="com.studytrails.tutorials.springremotingrmiserver.GreetingServiceImpl" />
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="greetingService" />
<property name="service" ref="greetingService" />
<property name="serviceInterface" value="com.studytrails.tutorials.springremotingrmiserver.GreetingService"/>
<property name="registryPort" value="1099" />
</bean>
</beans>
Client 端:springremotingrmiclient
package com.studytrails.tutorials.springremotingrmiserver;
//和server的一样
public interface GreetingService {
String getGreeting(String name);
}
package com.studytrails.tutorials.springremotingrmiclient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.studytrails.tutorials.springremotingrmiserver.GreetingService;
public class TestSpringRemotingRmi {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-client.xml");
GreetingService greetingService = (GreetingService)context.getBean("greetingService");
String greetingMessage = greetingService.getGreeting("Alpha");
System.out.println("The greeting message is : " + greetingMessage);
}
}
spring-config-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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="greetingService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/greetingService"/>
<property name="serviceInterface" value="com.studytrails.tutorials.springremotingrmiserver.GreetingService"/>
</bean>
</beans>
先启动Server, 后运行Client, 输出:
The greeting message is : Hello Alpha!
目录结构: