spring rmi使用心得

来源:互联网 发布:易语言鼠标点击源码 编辑:程序博客网 时间:2024/05/09 22:47

公司现在接了一个项目和以前的一个项目有很多相似的地方,为了避免业务逻辑重新开发,顾使用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 0
原创粉丝点击