rmi搭建项目(一)

一、写一个提供rmi服务的接口及实现;

创建如下包结构:


TestRmiAble:

package com.szy.project.rmi.able;

public interface TestRmiAble {
	
	String getTestRmiServerInfo();
}

TestRmiService:

package com.szy.project.rmi;

import com.szy.project.rmi.able.TestRmiAble;

public class TestRmiService implements TestRmiAble{

	public String getTestRmiServerInfo() {
		return "This is szy's Project's TestRmiServiceImpl!";
	}

}

二、rmi搭建过程中web.xml的配置;

在web.xml中添加配置:

 <!-- RMI远程服务  -->
	  <servlet>
	    <servlet-name>testDriver</servlet-name>
	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	    <load-on-startup>1</load-on-startup>
	  </servlet>
	  <servlet-mapping>
	    <servlet-name>testDriver</servlet-name>
	    <url-pattern>/testDriver/*</url-pattern>
	  </servlet-mapping>

三、rmi搭建过程中在web-inf中添加文件:

testDriver-servlet.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-3.2.xsd">
       

	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/TestServer">TestServer</prop>
			</props>
		</property>
	</bean>
</beans>

四、在src/main/resource下的applicationContext.xml中添加:

<!-- 远程服务服务器端实现类 -->
	<bean id="TestService" class="com.szy.project.rmi.TestRmiService" />
	<!-- 远程服务服务器端实现类 注册为HTTP invoker服务-->
	<bean id="TestServer" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
		<property name="service" ref="TestService" /><!-- 服务器端实现类 -->
		<property name="serviceInterface" value="com.szy.project.rmi.able.TestRmiAble"/>
	</bean>

test-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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/mvc     
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	<!--form客户端访问  -->
	<bean id="testServer"
		class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
		<property name="serviceUrl">
			<value>http://localhost:9090/Project/testDriver/TestServer</value>
		</property>
		<property name="serviceInterface" value="com.szy.project.rmi.able.TestRmiAble">
		</property>
	</bean>
</beans>

五、测试类TestRmiClient:

package com.szy.project.rmi.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.szy.project.rmi.able.TestRmiAble;

public class TestRmiClient {

	private static ApplicationContext context;
	
	private static TestRmiAble testRmiServer;
	
	public static TestRmiAble getTestRmiServer(){
		if(testRmiServer == null){
			context = new ClassPathXmlApplicationContext("test-client.xml");
			testRmiServer = (TestRmiAble)context.getBean("testServer");
		}
		return testRmiServer;
	}
	public static void main(String[] args) {
		System.out.println(getTestRmiServer().getTestRmiServerInfo());
	}
	
}

运行main方法得到如下信息:


整个rmi服务搭建完成!!!

此时在其他项目中要用这个接口时:


将TestRmiAble.java、TestRmi.java和test-client.xml拷贝出来,在项目中直接用效果是一样的;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值