Spring + RMI
服务端:
RmiServer.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- RMI远程调用 --> <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="serviceName" value="MyRMIService" /> <property name="service" ref="rmiService" /> <property name="serviceInterface" value="com.test.ddy.service.IRMIService" /> <property name="registryPort" value="8088" /> </bean> <bean id="rmiService" class="com.test.ddy.service.RMIServiceImpl" /></beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <description>Spring公共配置</description> <!-- 自动扫描 -->
<context:component-scan base-package="com.test.ddy" />
<mvc:annotation-driven />
<context:annotation-config /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<import resource="RmiServer.xml" />
<import resource="spring-mybatis.xml" />
</beans>
定义服务接口:
public interface IRMIService {
String getUserName(String name);
}
定义实现类:
package com.test.ddy.service;
import org.apache.log4j.Logger;
public class RMIServiceImpl implements IRMIService {
private static final Logger logger = Logger.getLogger(RMIServiceImpl.class);
@Override
public String getUserName(String name) {
// TODO Auto-generated method stub
return "hello,"+name;
}
}
客户端:
RmiServer.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- RMI远程调用 -->
<bean id="iRMIService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:8088/MyRMIService" />
<property name="serviceInterface" value="com.test.ddy.service.IRMIService" />
</bean>
</beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<description>Spring公共配置</description>
<!-- 自动扫描 -->
<context:component-scan base-package="com.test.ddy" />
<mvc:annotation-driven />
<context:annotation-config />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
<import resource="spring-mybatis.xml"/>
</beans>
客户端的spring-mvc.xml中不需要引入RmiServer.xml
定义接口:
package com.test.ddy.service;
public interface IRMIService {
String getUserName(String name);
}
客户端不需要实现这个接口,
客户端Controller:
package com.test.ddy.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.test.ddy.model.User;
import com.test.ddy.service.IRMIService;
import com.test.ddy.service.IUserService; /**
*/
@Controller
@RequestMapping("/index")
public class IndexController { @RequestMapping(value = "/getRMI", method = RequestMethod.GET)
public String getRMI(HttpServletRequest request) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:RmiServer.xml");
IRMIService hs = (IRMIService) ctx.getBean("iRMIService");
String temp = hs.getUserName("my vincent ");
System.out.println(temp);
return "index";
}
}
客户端控制台输出结果:hi my vincent
如果要传递一个User对象,则需要进行序列化User implements Serializable
服务端:github https://github.com/vincentduan/SpringRMIServer.git
客户端:https://github.com/vincentduan/SpringRMIClient.git
Spring + RMI的更多相关文章
- Spring RMI (Spring 远程方法调用)
所需jar包...? 不纠结,一股脑儿全导! 源码地址:http://pan.baidu.com/s/1jG8eOmy 先放结构图如下,客户端和服务端都在一个项目中.也可以把服务端的xxx导成j ...
- Spring RMI (Spring 远程方法调用)【原】
所需jar包...? 不纠结,一股脑儿全导! 源码地址:http://pan.baidu.com/s/1jG8eOmy 先放结构图如下,客户端和服务端都在一个项目中.也可以把服务端的xxx导成j ...
- spring RMI的使用
Spring整合RMI的原理 客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性. 通过JRMP访问服务.JRMP JRMP:ja ...
- Spring RMI Example
一: 提供服务的远程一端 1-1. applicationContext.xml <?xml version="1.0" encoding="UTF-8" ...
- Spring Remoting: Remote Method Invocation (RMI)--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...
- Spring之RMI 远程方法调用 (Remote Method Invocation)
RMI 指的是远程方法调用 (Remote Method Invocation) 1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程 ...
- Spring之rmi实例演示
环境介绍:本文中服务端客户端使用的都是ssm框架,配置文件分为spring_servlet.xml,spring_service.xml,mybatis.xml 在spring里面使用rmi完成远程调 ...
- spring源码分析之spring-web remoting模块概况及基本概念
spring-web总体分为三部分:caucho.httpinvoker.jaxws,其总体构造图如下: uml结构: 先看看网上搜索到的上述实现的原理吧:Spring RMI,Hessian/Bur ...
- RMI学习
前段时间学习JMX,知道可以使用rmi连接器,就顺便看下rmi是什么东西,RMI 全称Remote Method Invocation-远程方法调用,实现远程对象之间的调用,下面原理图来自网络 服务器 ...
随机推荐
- jq打印
1.引入jQuery.print.min.js 2.将需要打印的东西用div包起来 3. $(".printDiv").print();
- python 字符与字节 json序列和反序列及支持的类型
b = b"demo" s = "demo" # 字符串转字节 s = bytes(s, encoding = "utf8") s = st ...
- uva1412 Fund Management
状压dp 要再看看 例题9-17 /* // UVa1412 Fund Management // 本程序会超时,只是用来示范用编码/解码的方法编写复杂状态动态规划的方法 // Rujia Liu ...
- Ztree 多选,显示勾选的路径
项目要求,需要向后台传递已经勾选的路径,如 l1-a, l1-l3-c,l1-l3-d;(如果是全选状态则只传递全选状态的路径,不传子节点). 具体可以参考jQ Ztree 的 v3.5 版本 Me ...
- C/C++连接MySQL数据库执行查询
1. 简介: 使用C/C++连接MySQL数据库执行增删改查操作,基本就是围绕以下两个文件展开: mysql.h(此头文件一般在MySQL的include文件夹内,如 D:\MySQL\mysql-5 ...
- jdk环境变量配置至第一个简单程序运行成功
桌面右键单击我的电脑,属性,高级,环境变量,然后再系统变量中配置(也可在用户变量中配置) 在配置环境变量时限查看是否已存在变量名称,有则添加路径,没有则创建再添加路径 /* JAVA_HOME% C: ...
- Struts2入门(1)——搭建简单的环境
步骤: 1.下载Struts2的开发包. 2.创建Web项目. 3.导入需要的jar包到项目里. 4.在web.xml文件里面配置struts2的核心控制器,也就是一个过滤器. 5.编写Action类 ...
- java环境配置——配置tomcat用户
Tomcat Manager是Tomcat自带的.用于对Tomcat自身以及部署在Tomcat上的应用进行管理的web应用.Tomcat是Java领域使用最广泛的服务器之一,因此Tomcat Mana ...
- python模块以及导入出现ImportError: No module named ‘xxx‘问题
python中,每个py文件被称之为模块,每个具有__init__.py文件的目录被称为包.只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用如果你要使 ...
- URAL 1277 Cops and Thieves
Cops and Thieves Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...