步骤如下:
第一创建两个接口:
Person接口代表人,Axe接口代表斧头
Person.java源代码如下:
第二:创建接口实现类:
Chiness具体类实现接口Person,
SteelAxe具体类实现接口Axe
第三:创建服务器端的启动服务类:StartService.java
package Server;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StartService {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println("============================");
}
}
第四:创建客户端测试类Test.java
package client;
import com.remote.Person;
public class Test {
private Person person;
public void setPerson(Person person)
{
this.person=person;
}
public void test(){
System.out.println("client.Test test():"+person.useAxe());
}
}
第五:创建客户端入口程序BeanTest.java
package client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanTest {
public static void main(String[]args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("clientBean.xml");
Test test=(Test)ctx.getBean("test");
test.test();
}
}
第五:在服务器端创建applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="steelAxe" class="com.remote.impl.SteelAxe"/>
<bean id="chinese" class="com.remote.impl.Chinese" >
<property name="axe">
<ref bean="steelAxe"/>
</property>
</bean>
<!-- 使用RmiServiceExporter将目标bean暴露成远程服务 -->
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- 指定暴露出来的远程服务名 -->
<property name="serviceName">
<value>chineseRemote</value>
</property>
<!-- 配置需要暴露的目标bean -->
<property name="service">
<ref bean="chinese"/>
</property>
<!-- 配置需要暴露的目标bean所实现的接口 -->
<property name="serviceInterface">
<value>com.remote.Person</value>
</property>
<property name="registryPort">
<value>1099</value>
</property>
</bean>
</beans>
第六:在客户端创建clientBean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 配置测试bean -->
<bean id="test" class="client.Test">
<!-- test Bean 依赖于远程服务 -->
<property name="person">
<ref bean="person"/>
</property>
</bean>
<!-- 通过RmiProxyFactoryBean访问远程服务 -->
<bean id="person" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<!-- 指定远程服务的url,后的服务名应与服务器端的服务名相同 -->
<property name="serviceUrl">
<value>rmi://localhost/chineseRemote</value>
</property>
<!-- 指定远程服务的接口名 -->
<property name="serviceInterface">
<value>com.remote.Person</value>
</property>
</bean>
</beans>
运行:
1.先运行StartService.java。启动服务器
控制台打印:
Spring init Chinese()!
============================
并且程序并没有结束!
2.再运行BeanTest.java.结果如下:
client.Test test():Steel Axe is very Sharp