Spring使用远程服务之Hessian
来源:互联网 发布:caj viewer for mac 编辑:程序博客网 时间:2024/05/07 01:20
Hessian像RMI一样,使用二进制消息进行客户端和服务端的交互,它的二进制消息可以移植到其他非Java的语言中包括PHP、Python、C++和C#。因为Hessian是基于HTTP的,所以HessianSeriviceExporter实现为一个Spring MVC控制器。
HessianSeriviceExporter是一个SpringMVC控制器,它可以接收Hessian请求,并将这些请求翻译成对POJO的调用来,从而将POJO导出为一个Hessian服务
为了使用导出Hessian服务,我们需要执行两个额外的配置步骤:
1、在web.xml中配置Spring的DispatcherServlet,并把我们的应用部署为Web应用;
2、在Spring的配置文件中配置一个URL处理器,将Hessian服务的URL分发给对应的Hessian服务Bean。
下面我们距离说明
我的项目分层
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <!-- 在此处配置刚刚写的spring-hessian.xml的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/spring-hessian.xml </param-value> </context-param> <servlet> <!-- servlet-name保持与spring-hessian.xml中一致 --> <servlet-name>HelloServiceExporter</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServiceExporter</servlet-name> <url-pattern>/HelloService</url-pattern> </servlet-mapping> </web-app>
spring-hessian.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:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"default-lazy-init="true"> <context:annotation-config /> <!-- 组件扫描,使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 --><context:component-scan base-package="hessian" /><!-- 自动装配 --><bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <!-- Name保持与web.xml中的一致,web.xml下文中描述 --> <bean name="HelloServiceExporter" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- service的ref与HelloServiceImpl中@Service中配置的一致 --> <property name="service" ref="helloService" /> <!-- 接口的路径 --> <property name="serviceInterface" value="hessian.HelloService" /> </bean> </beans>
java类
package hessian;public interface HelloService {void sayHello(String name);}
package hessian;import org.springframework.stereotype.Service;@Service("helloService") public class HelloServiceImpl implements HelloService { /* (non-Javadoc) * @see com.gsoft.geloin.service.HelloService#sayHello(java.lang.String) */ @Override public void sayHello(String name) { System.out.println("Hello " + name + "!"); } }
客户端的调用
首先将Service的打成jar包加入到客户端程序中
测试代码
package hessionclient;import hessian.HelloService;import com.caucho.hessian.client.HessianProxyFactory;public class ArticleManager {public static void main(String[] args) { try { String url = "http://localhost:8080/SpringTest/HelloService"; HessianProxyFactory factory = new HessianProxyFactory(); HelloService helloService = (HelloService) factory.create( HelloService.class, url); helloService.sayHello("张三"); } catch (Exception e) { e.printStackTrace(); } } }
客户端调用图
参考:
《Spring实战(第3版)》
PS:代码主体来源于网络
3 1
- Spring使用远程服务之Hessian
- Spring使用远程服务之Hessian
- (二)spring 远程服务之Hessian
- 远程服务调用之RMI、Hessian、Burlap、Spring的HTTPinvoker
- 使用Hessian提供远程服务
- Spring整合Hessian访问远程服务
- Spring整合Hessian访问远程服务
- Spring整合Hessian访问远程服务 - OnTheRoad_Lee
- hessian远程调用及spring中使用hessian
- java远程调用之spring和hessian
- Spring使用Hessian实现远程调用
- Hessian-远程服务调用
- Spring中使用Hessian暴露服务
- Spring学习笔记之使用远程服务
- [经典转载]Spring整合Hessian访问远程服务
- spring hessian远程调用
- Spring框架学习【Spring使用Hessian实现远程调用】
- spring技术内幕18-Spring使用Hessian实现远程调用
- android 锁屏独立壁纸
- 美国领事馆国内领区分布
- OpenCV Machine Learning 之 K最近邻分类器的应用 K-Nearest Neighbors
- Log与logcat
- Android中软件键盘的管理
- Spring使用远程服务之Hessian
- MFC双缓冲显示位图
- Scrapy安装介绍
- Apple Swift编程语言入门教程
- 游标越界
- test Week 13
- hashmap代码解析
- 第一篇博文——为什么要写
- poj1062--昂贵的聘礼