Hessian入门(与Spring集成)
2019独角兽企业重金招聘Python工程师标准>>>
Hessian入门(与Spring集成)
By:wtang
说明 :
1. 讲述如何配置Hessian的服务器端(与Spring集成).
2. 讲述客户端如何调用
① 使用HessianProxyFactory Hessian代理工厂直接调用
② 使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用.
1. 讲述如何配置Hessian的服务器端(与Spring集成).
接口定义类: com.wtang.isay. Isay:
- package com.wtang.isay;
- public interface Isay {
- public String sayHello(String arg1,String arg2);
- }
package com.wtang.isay;public interface Isay { public String sayHello(String arg1,String arg2);}</pre>
接口具体实现类: com.wtang.isay. IsayImpl
- package com.wtang.isay;
- public class IsayImpl implements Isay {
- public String sayHello(String arg1, String arg2) {
- return "Hello:" + arg1 + arg2;
- }
- }
package com.wtang.isay;public class IsayImpl implements Isay { public String sayHello(String arg1, String arg2) { return "Hello:" + arg1 + arg2; }}</pre>
配置Web.xml:
- <servlet>
- <servlet-name>remote</servlet-name>
- <!-- 使用Spring的代理Servlet -->
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>namespace</param-name>
- <param-value>classes/remote-servlet</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>remote</servlet-name>
- <url-pattern>/remote/*</url-pattern>
- </servlet-mapping>
<servlet> <servlet-name>remote</servlet-name> <!-- 使用Spring的代理Servlet --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name> <param-value>classes/remote-servlet</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remote</servlet-name> <url-pattern>/remote/*</url-pattern> </servlet-mapping></pre>
配置remote-servlet.xml[该文件位于src目录下,即编译后存在与classes下]:
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!-- 接口的具体实现类 -->
- <bean id="impl" class="com.wtang.isay.IsayImpl" />
- <!-- 使用Spring的HessianServie做代理 -->
- <bean name="/helloSpring"
- class="org.springframework.remoting.caucho.HessianServiceExporter">
- <!-- service引用具体的实现实体Bean-->
- <property name="service" ref="impl" />
- <property name="serviceInterface" value="com.wtang.isay.Isay" />
- </bean>
- <!-- 可以配置多个HessianServiceExporter代理Bean -->
- </beans>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!-- 接口的具体实现类 --> <bean id="impl" class="com.wtang.isay.IsayImpl" /> <!-- 使用Spring的HessianServie做代理 --> <bean name="/helloSpring" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- service引用具体的实现实体Bean--> <property name="service" ref="impl" /> <property name="serviceInterface" value="com.wtang.isay.Isay" /> </bean> <!-- 可以配置多个HessianServiceExporter代理Bean --></beans></pre>
注:
这个文件为什么叫remote-servlet.xml呢?
因为我们在web.xml中有配置:<servlet-name>remote</servlet-name>。
所以remote-servlet.xml的文件名必须以
<servlet-name>中配置的servlet-name作为文件名的开头,
且文件名的格式必须是[servlet-name]-servlet.xml格式,否则检测不到。
即:
<param-value>classes/remote-servlet</param-value>
所以文件名为remote-servlet.xml。
2. 讲述客户端如何调用
① 使用HessianProxyFactory Hessian代理工厂直接调用
即:
- package com.wtang.test;
- import java.net.MalformedURLException;
- import com.caucho.hessian.client.HessianProxyFactory;
- import com.wtang.isay.Isay;
- public class NormalClient {
- public static void main(String[] args) throws MalformedURLException {
- //Spring Hessian代理Servelet
- String url = "http://localhost:8080/HessianSpring/remote/helloSpring";
- HessianProxyFactory factory = new HessianProxyFactory();
- Isay api = (Isay) factory.create(Isay.class, url);
- System.out.println(api.sayHello("chen", "weitang"));
- }
- }
package com.wtang.test;import java.net.MalformedURLException;import com.caucho.hessian.client.HessianProxyFactory;import com.wtang.isay.Isay;public class NormalClient { public static void main(String[] args) throws MalformedURLException { //Spring Hessian代理Servelet String url = "http://localhost:8080/HessianSpring/remote/helloSpring"; HessianProxyFactory factory = new HessianProxyFactory(); Isay api = (Isay) factory.create(Isay.class, url); System.out.println(api.sayHello("chen", "weitang")); }}</pre>
输出Hello:chenweitang
2. 讲述客户端如何调用
② 使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用.
配置客户端 remote-client.xml:
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!-- 客户端Hessian代理工厂Bean -->
- <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
- <!-- 请求代理Servlet路径 -->
- <property name="serviceUrl">
- <value>http://localhost:8080/HessianSpring/remote/helloSpring</value>
- </property>
- <!-- 接口定义 -->
- <property name="serviceInterface">
- <value>com.wtang.isay.Isay</value>
- </property>
- </bean>
- </beans>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!-- 客户端Hessian代理工厂Bean --> <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <!-- 请求代理Servlet路径 --> <property name="serviceUrl"><value>http://localhost:8080/HessianSpring/remote/helloSpring</value> </property> <!-- 接口定义 --> <property name="serviceInterface"> <value>com.wtang.isay.Isay</value> </property> </bean></beans></pre>
调用:
- package com.wtang.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.wtang.isay.Isay;
- public class SpringClient {
- public static void main(String[] args) {
- ApplicationContext contex = new ClassPathXmlApplicationContext(
- "remote-client.xml");
- // 获得客户端的Hessian代理工厂bean
- Isay i = (Isay) contex.getBean("clientSpring");
- System.out.println(i.sayHello("chen", "weitang"));
- }
- }
package com.wtang.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wtang.isay.Isay;public class SpringClient { public static void main(String[] args) { ApplicationContext contex = new ClassPathXmlApplicationContext( "remote-client.xml"); // 获得客户端的Hessian代理工厂bean Isay i = (Isay) contex.getBean("clientSpring"); System.out.println(i.sayHello("chen", "weitang")); }}</pre>
输出Hello:chenweitang
转载于:https://my.oschina.net/201003674/blog/650263
Hessian入门(与Spring集成)相关推荐
- Kafka 入门和 Spring Boot 集成
2019独角兽企业重金招聘Python工程师标准>>> Kafka 入门和 Spring Boot 集成 概述 kafka 是一个高性能的消息队列,也是一个分布式流处理平台(这里的流 ...
- Spring集成JMS入门
1.预备知识 在学习Spring集成JMS之前最好可以是先去了解JMS基本概念和ActiveMQ 自己学习Spring整合JMS的一些心得,希望可以帮到大家 2.Spring整合JMS 2.1.使用点 ...
- 函数集成redis与Spring集成
新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正 redis与Spring集成比较简单,本文不触及Spring的一些基本概念,读都需先具有Spring的相干知识. 先在maven中添 ...
- jersey spring_实施Jersey 2 Spring集成
jersey spring Jersey是Oracle提供的出色的Java JAX-RS规范参考实现. 去年,当我们开始为大容量网站构建RESTful后端Web服务时,我们选择使用JAX-RS API ...
- 实施Jersey 2 Spring集成
Jersey是Oracle提供的出色的Java JAX-RS规范参考实现. 去年,当我们开始为大容量网站构建RESTful后端Web服务时,我们选择使用JAX-RS API作为我们的REST框架和Sp ...
- 引入Spring集成
在本文中,我们介绍Spring Integration . 如果您以前没有使用过Spring Integration,那么可能会帮助您复习Gregor Hohpe的Enterprise Integra ...
- 《springcloud超级入门》Spring Cloud和Dubbo的区别及各自的优缺点《三》
了解为什么需要微服务.最初的服务化解决方案是给相同服务提供一个统一的域名,然后服务调用者向这个域发送 HTTP 请求,由 Nginx 负责请求的分发和跳转. 这种架构存在很多问题:Nginx 作为中间 ...
- 阿里RocketMq试用记录+简单的Spring集成
CSDN学院招募微信小程序讲师啦 程序猿全指南,让[移动开发]更简单! [观点]移动原生App开发 PK HTML 5开发 云端应用征文大赛,秀绝招,赢无人机! 阿里R ...
- 亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像)
亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像) 大噶好,我们继续延续上一章,学习如何使用OSS存放用户头像代码示例; 在application.propert ...
最新文章
- 谢尔排序/缩减增量排序(C++)
- 数学知识笔记:拉格朗日乘子
- dropout理解(一)
- 操作篇 bgp协议了解与学习
- GIS应对新挑战——空间信息网格技术探寻
- 快速排序算法c语言lomuto,快速排序(N.Lomuto版)
- 支付宝蚂蚁森林入选2019年世界环境日实践案例
- spark学习-66-源代码:schedulerBackend和taskScheduler的创建(4)-yarn
- sql server 2005学习笔记之触发器简介(一)
- [VB]SaveSetting 语句 和 DeleteSetting 语句
- MySQL中的主键约束和外键约束
- [渝粤教育] 中国地质大学 管理信息系统 复习题
- 微软云 Azure 云服务器 Web应用服务云计算解决方案
- WebApp最佳实践用户体验篇:针对多种屏幕尺寸合理设计
- python 电路仿真spice_SPICE模型电路仿真器的用法及功能解析
- JS解决因循环绑定click事件失效
- OSAL 之功耗管理
- Whitelabel Error Page 的原因
- 步入J2EE架构和过程
- python namedtuple默认值_python 使用 namedtuple