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
- Spring RMI使用心得
- spring rmi使用心得
- Spring-remoting使用心得1-RMI
- spring rmi 的使用
- spring rmi 简单使用
- 使用spring配置RMI
- Spring RMI使用学习
- Spring使用POJO发布RMI
- RED5 中使用Spring RMI
- spring与rmi结合使用
- spring使用心得
- spring事务管理使用心得
- Spring MVC 使用心得
- spring定时器使用心得
- Spring Cloud使用心得
- Spring使用心得
- 使用Spring RMI调用远程方法
- Spring整合使用RMI-HelloWorld例子
- cocos2d-x 读取xml中的“\n”不能换行
- 2013.12.28 学习九九乘法表
- Least Common Multiple
- 计算机为什么能生成随机数?
- oracle的number类型精度、刻度范围
- spring rmi使用心得
- 运算符重载
- 关于HOOK浏览器NtDeviceIoControlFile函数,修改POST数据的问题
- 2013.12.29 Java--函数(重载)
- ffmpeg与新合成程序效率对比测试
- Python的print用法
- 质因数能分解出的最高个数
- 2013.12.29 静态初始化一个数组
- 2013.12.29 数组常见操作:遍历