1.生成服务接口
package com.yyh.rmi;
public interface IHello {
public String hello(String name);
}
2.生成服务接口实现
package com.yyh.rmi;
public class HelloImp implements IHello{
public String hello(String name){
return "hello:" + name;
}
}
3.生成RmiServer程序
package com.yyh.rmi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiServer {
public static void main(String[] args) {
//String fs = System.getProperties().getProperty("file.separator");
ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-server.xml");
}
}
4.生成RmiClient程序
package com.yyh.rmi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class RMIClient {
public static void main(String[] args) {
//ApplicationContext content = new FileSystemXmlApplicationContext("src/conf/biz/spring/spring-client.xml");
ApplicationContext content = new ClassPathXmlApplicationContext("spring/spring-client.xml");
IHello iHello = (IHello)content.getBean("serviceClient");
System.out.println(iHello.hello("yuanhuyao"));
}
}
5.服务器端服务配置文件spring-server.xml
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
" http://www.springframework.org/dtd/spring-beans.dtd
">
<beans>
<bean id="helloService" class="com.yyh.rmi.HelloImp"/>
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service">
<ref bean="helloService"/>
</property>
<!-- 定义服务名 -->
<property name="serviceName">
<value>hello</value>
</property>
<property name="serviceInterface">
<value>com.yyh.rmi.IHello</value>
</property>
<!-- defaults to 1099 -->
<property name="registryPort">
<value>8888</value>
</property>
</bean>
</beans>
6.客户端服务配置文件spring-client.xml
<?xml version="1.0" encoding="GB2312"?>
<beans xmlns=" http://www.springframework.org/schema/beans "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring
- beans-2.0.xsd">
<bean id="serviceClient"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface">
<value>com.yyh.rmi.IHello</value>
</property>
<!-- serviceUrl以rmi开头,定义服务器地址与端口和服务名 -->
<property name="serviceUrl">
<value>rmi://localhost:8888/hello</value>
</property>
</bean>
</beans>
7.开发环境设置
新建项目文件spring-rmi
程序建在/home/yuanhuyao/work/spring-rmi/src/java/com/yyh/rmi/目录下;
配置文件建在/home/yuanhuyao/work/spring-rmi/src/conf/biz/spring/目录下;
eclipse中在该项目中增加class fold:spring-rmi/src/conf/biz (如果设置不对回找不到对应的XML配置文件)
在该项目中导入三方库spring-2.0.1.jar
8.运行
运行RmiServer选“Rmi application”
运行RmiClient选"Java application"
package com.yyh.rmi;
public interface IHello {
public String hello(String name);
}
2.生成服务接口实现
package com.yyh.rmi;
public class HelloImp implements IHello{
public String hello(String name){
return "hello:" + name;
}
}
3.生成RmiServer程序
package com.yyh.rmi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiServer {
public static void main(String[] args) {
//String fs = System.getProperties().getProperty("file.separator");
ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-server.xml");
}
}
4.生成RmiClient程序
package com.yyh.rmi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class RMIClient {
public static void main(String[] args) {
//ApplicationContext content = new FileSystemXmlApplicationContext("src/conf/biz/spring/spring-client.xml");
ApplicationContext content = new ClassPathXmlApplicationContext("spring/spring-client.xml");
IHello iHello = (IHello)content.getBean("serviceClient");
System.out.println(iHello.hello("yuanhuyao"));
}
}
5.服务器端服务配置文件spring-server.xml
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
" http://www.springframework.org/dtd/spring-beans.dtd
<beans>
<bean id="helloService" class="com.yyh.rmi.HelloImp"/>
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service">
<ref bean="helloService"/>
</property>
<!-- 定义服务名 -->
<property name="serviceName">
<value>hello</value>
</property>
<property name="serviceInterface">
<value>com.yyh.rmi.IHello</value>
</property>
<!-- defaults to 1099 -->
<property name="registryPort">
<value>8888</value>
</property>
</bean>
</beans>
6.客户端服务配置文件spring-client.xml
<?xml version="1.0" encoding="GB2312"?>
<beans xmlns=" http://www.springframework.org/schema/beans
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=" http://www.springframework.org/schema/beans
<bean id="serviceClient"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface">
<value>com.yyh.rmi.IHello</value>
</property>
<!-- serviceUrl以rmi开头,定义服务器地址与端口和服务名 -->
<property name="serviceUrl">
<value>rmi://localhost:8888/hello</value>
</property>
</bean>
</beans>
7.开发环境设置
新建项目文件spring-rmi
程序建在/home/yuanhuyao/work/spring-rmi/src/java/com/yyh/rmi/目录下;
配置文件建在/home/yuanhuyao/work/spring-rmi/src/conf/biz/spring/目录下;
eclipse中在该项目中增加class fold:spring-rmi/src/conf/biz (如果设置不对回找不到对应的XML配置文件)
在该项目中导入三方库spring-2.0.1.jar
8.运行
运行RmiServer选“Rmi application”
运行RmiClient选"Java application"