Hessian Spirng实例
Spring实例
之前,我们做了很简单的纯Hessian的调用,虽然到此已经能够满足远程调用的需求了,但是我听说spring也能够访问hessian的远程服务,研究了一番,废话不多说,直接上示例。
业务场景
servlet的例子并未涉及到复杂对象的传输,这次我们搞复杂点,设计一个服务,通过远程调用的方式来找爸爸的儿子。
服务端
环境搭建
引入hessian、spring-mvc的相关jar包,后面会附上相关的pom文件配置,项目结构如下:
示例代码
复杂的对象传输时,只需要类继承Serializable,保证在数据传输时能够序列化和反序列化,如下面的Father和Child类。
父亲:
package example; import java.io.Serializable; /** * @author X */ public class Father implements Serializable { private static final long serialVersionUID = 1L; public Father(String name) { this.name = name; } private String name; public String getName() { return name; } }
Father.java
儿子:
package example; import java.io.Serializable; /** * @author X */ public class Child implements Serializable { private static final long serialVersionUID = 1L; public Child(String name) { this.name = name; } private String name; public String getName() { return name; } }
Child.java
接送接口:
package example; /** * @author X */ public interface ShuttleService { String getCar(); Child getChild(Father father); }
ShuttleService.java
接送实现:
package example; /** * @author X */ public class ShuttleServiceImpl implements ShuttleService { public String getCar() { return "小火车"; } public Child getChild(Father father) { if (father != null) return new Child(father.getName() + "的儿子"); return null; } }
ShuttleServiceImpl.java
spring配置:
<?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.xsd"> <bean id="shuttle" class="example.ShuttleServiceImpl"/> <bean name="/shuttleService" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service"> <ref bean="shuttle"/> </property> <property name="serviceInterface"> <value>example.ShuttleService</value> </property> </bean> </beans>
hessian-spring.xml
web配置:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Demo</display-name> <servlet> <servlet-name>shuttle</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/hessian-spring.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>shuttle</servlet-name> <url-pattern>/rpc/*</url-pattern> </servlet-mapping> </web-app>
web.xml
依赖配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId>container</artifactId> <groupId>hessian.host</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring</artifactId> <packaging>war</packaging> <name>spring Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.0.6.RELEASE</version> </dependency> </dependencies> <build> <finalName>spring</finalName> </build>
pom.xml
客户端
环境搭建
引入hessian、spring-mvc的相关jar包,项目结构如下:
调用:
import example.Father; import example.ShuttleService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.net.MalformedURLException; /** * @author X */ public class Run { public static void main(String[] args) throws MalformedURLException, ClassNotFoundException { ApplicationContext context = new ClassPathXmlApplicationContext("spring-rpc.xml"); ShuttleService ss = (ShuttleService) context.getBean("rpcClient"); System.out.println(ss.getCar()); System.out.println(ss.getChild(new Father("王老二")).getName()); } }
Run.java
客户端spring配置:
<?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.xsd"> <bean id="rpcClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <value>http://localhost:45/rpc/shuttleService</value> </property> <property name="serviceInterface"> <value>example.ShuttleService</value> </property> </bean> </beans>
spring-rpc.xml
运行后输出结果如下:
注意在实例中我是把Father和Child和ShuttleService分别定义了两次,实际开发中不建议这样做,把需要调用的部分作为一个公共API供分别提供给客户端和服务端使用,否则可能会照成反序列化失败。
把spring和hessian相结合后,无论是服务端还是客户端的业务代码中,已经没有一行与hessian相关的代码了,也就是说spring让我们的开发无关远程接口的实现了,这样我们就可只关注于开发而不必去关注远程调用怎么去实现。如果说我们哪天需要切换另外一个spring支持的远程访问接口,也只需要修改下配置文件就搞定了,so easy,妈妈再也不用担心我的实现代码了!
Hessian Spirng实例的更多相关文章
- Hessian Servlet实例
Servlet实例 业务场景 在下面的例子中我会发布一个简单的输出字符串的方法,然后在客户端调用并输出结果. 服务器端 环境搭建 在服务端,我们需要引入hessian和servlet的包.编写服务.配 ...
- WebService另一种轻量级实现—Hessian 学习笔记
最近和同事聊天,得知他们在使用一种叫做Hessian的WebService实现方式实现远 程方法调用,是轻量级的,不依赖JavaEE容器,同时也是二进制数据格式传输,效率比SOAP的XML方式要高.感 ...
- 最近学习工作流 推荐一个activiti 的教程文档
全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...
- Spring+Hessian+Maven+客户端调用实例
Hessian是一个采用二进制格式传输的服务框架,相对传统soap web service,更轻量,更快速.官网地址:http://hessian.caucho.com/ 先上个效果图,在客户端界面通 ...
- Hessian实例
简述Hessian Hessian是一个由Caucho Technology开发的轻量级RPC框架,由于它使用二进制RPC协议,所以它更快.更简单,很适合于发送二进制数据(访问官网): 在进行基于He ...
- Hessian最佳实践
前言:本文主要介绍‘独立的Hessian技术’与‘结合Spring技术’的两种Hessian接口开发模式及代码示例. 一.独立的Hessian技术开发步骤 Hessian-Java服务器端必须具备以下 ...
- Java学习之Hessian通信基础
一.首先先说Hessian是什么? Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简 ...
- JAVA上百实例源码以及开源项目
简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级.中级.高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情.执着,对IT的憧憬. ...
- Spring单实例、多线程安全、事务解析
原文:http://blog.csdn.net/c289054531/article/details/9196053 引言: 在使用Spring时,很多人可能对Spring中为什么DAO和Se ...
随机推荐
- the selection cannot be run on any server
导入war包后运行jsp 显示: the selection cannot be run on any server 问题原因: Dynamic Web Module 的版本与server不匹配.Dy ...
- 织梦dedecms红黑配图片模板源码v2.0
dedecms红黑配风格美女图片站是采用dedecms程序搭建的图片网站源码,网站感觉很大气,简约但是不简单,适合做图片网站.网站模板是收集其他网站的模板,感谢原网站提供者.在安装过程中出现问题,现已 ...
- 更换WordPress后台登录地址
在后台找到wp-content—themes—twentyfifteen(当前的网站主题)—functions.php 在代码的最下面加入以下代码: //后台唯一登录地址 add_action('lo ...
- 继承&封装
扩展一个已有的类,并且继承该类的属性和行为这种方式成为继承. 实例 public class Polygon { public int sides; public double area; publi ...
- Qwt--散点图/函数图
1.Qwt库 QwtPlot是用来绘制二维图像的widget.在它的画板上可以无限制的显示绘画组件.绘画组件可以是曲线(QwtPlotCurve).标记(QwtPlotMarker).网格(QwtPl ...
- 在maven项目结构下对于Resources目录下文件的存取
在maven项目中,文件结构如下: proj ---src ----main ----java ----Main.java ----resources ----userFile.properties ...
- B站真的是一个神奇的地方,初次用Python爬取弹幕。
"网上冲浪""886""GG""沙发"--如果你用过这些,那你可能是7080后: "杯具"" ...
- Matrix Matcher UVA - 11019AC_自动机 + 代价提前计算
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<vector> ...
- 【python正则】工作中常用的python正则代码
工作中常用的一些正则代码: 01.用户名正则 import re # 4到16位(字母,数字,下划线,减号)if re.match(r'^[a-zA-Z0-9_-]{4,16}$', "ab ...
- GOF23设计模式之建造者模式
GOF23设计模式之建造者模式 场景: 我们要建造一个复杂的产品.比如:神州飞船,Iphone.这个复杂的产品的创建.有这样的一个问题需要处理: 装配这些子组件是不是有个步骤问题? 实际开发中,我们所 ...