taotaoSi 2009-10-14
package com.spring.rmi; public interface RMIService { public void show(); public String getString(String str); }
package com.spring.rmi.impl; import com.spring.rmi.RMIService; public class RMIServiceImpl implements RMIService{ public String getString(String str) { return (str + "访问成功!"); } public void show() { System.out.println("这是服务器端的show()方法!"); } }
<?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.5.xsd"> <bean id="service" class="com.spring.rmi.impl.RMIServiceImpl"> </bean> <bean id="exporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="service"></property> <property name="serviceName" value="service"></property> <property name="serviceInterface" value="com.spring.rmi.RMIService"></property> <property name="registryPort"> <value>2222</value> </property> </bean> </beans>
<?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.5.xsd"> <bean id="serviceProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://10.20.10.201:2222/service"></property> <property name="serviceInterface" value="com.spring.rmi.RMIService"> </property> </bean> </beans>
package testRMI; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.rmi.RMIService; import com.spring.rmi.impl.RMIServiceImpl; public class testRMI { public static void main(String args[]) { ApplicationContext context = new ClassPathXmlApplicationContext("RMI-server.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("rmi-client.xml"); RMIService server = (RMIService)context2.getBean("serviceProxy"); server.show(); System.out.println(server.getString("赵红伟")); } }
大家如果需要源文件可以给我留言!