spring rmi使用心得

公司现在接了一个项目和以前的一个项目有很多相似的地方,为了避免业务逻辑重新开发,顾使用spring rmi,把所有的bean作为rmi服务暴漏出来,在客户端只需要把项目依赖过来就ok,或者把以前的接口导入过来。

废话不多说,直接上源码,下面是项目结构图:


HelloRmiServiceImpl.java的内容如下:

package com.baidu.phl.rmi.service.impl;

import com.baidu.phl.rmi.service.HelloRmiService;

public class HelloRmiServiceImpl implements HelloRmiService {

	public int getAdd(int a, int b) {
		return a + b;
	}
}

ClientTest.java内容如下:

package com.baidu.phl.rmi.service.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.baidu.phl.rmi.service.HelloRmiService;

public class ClientTest {
	private static Log log = LogFactory.getLog(ClientTest.class);

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
		HelloRmiService hms = context.getBean("myClient", HelloRmiService.class);
		log.info(hms.getAdd(1, 2));
	}
	
}

ServerTest.java的内容如下:

package com.baidu.phl.rmi.service.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ServerTest {
	private static Log log = LogFactory.getLog(ServerTest.class);

	public static void main(String[] args) {
		//初始化工作只能运行一次;运行多次的话,会启动多个服务
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		log.info("hello commons-logging!");
	}
}

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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/aop  
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--服务端--> 
	<bean id="helloRmiServiceImpl" class="com.baidu.phl.rmi.service.impl.HelloRmiServiceImpl" scope="prototype"/>
	<!-- 将类为一个RMI服务 -->
	<bean id="myRmi" class="org.springframework.remoting.rmi.RmiServiceExporter">
		<!-- 服务类 -->
		<property name="service" ref="helloRmiServiceImpl" />
		<!-- 服务名 -->
		<property name="serviceName" value="helloRmi" />
		<!-- 服务接口 -->
		<property name="serviceInterface" value="com.baidu.phl.rmi.service.HelloRmiService" />
		<!-- 服务端口 -->
		<property name="registryPort" value="9999" />
		<!-- 其他属性自己查看org.springframework.remoting.rmi.RmiServiceExporter的类,就知道支持的属性了-->
	</bean>
</beans>

applicationContext2.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/aop  
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <!--客户端--> 
    <bean id="myClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 
        <property name="serviceUrl" value="rmi://127.0.0.1:9999/helloRmi"/> 
        <property name="serviceInterface" value="com.baidu.phl.rmi.service.HelloRmiService"/> 
    </bean> 
</beans>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值