当前位置: 首页 > news >正文

Spring 整合 Hessian

一:服务端

web.xml
	<!-- DispatcherServlet -->
	<servlet>
		<servlet-name>Spring-DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/applicationContext-hessianServer.xml</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>Spring-DispatcherServlet</servlet-name>
		<url-pattern>*.hessian</url-pattern>
	</servlet-mapping>
package com.xx.service;

/**
 * 服务接口 
 */
public interface ISayHelloService {

	/**
	 * @param name
	 * @return
	 */
	 String doSayHello(String name);
	 
	/**
	 * @param name
	 * @param welcomeStr
	 * @return
	 */
	 String doSayHello(String name, String welcomeStr);
}
package com.xx.service.impl;

import com.xx.service.ISayHelloService;

/**
 * 服务接口实现 
 */
public class DefaultSayHelloServiceImpl implements ISayHelloService {

	public String doSayHello(String name) {
		return doSayHello(name, "hello");
	}

	public String doSayHello(String name, String welcomeStr) {
		return name + "," + welcomeStr;
	}
}

 

spring配置文件applicationContext-hessianServer.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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.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-2.5.xsd"
		default-lazy-init="false">
	
	<!-- 服务端 实现-->
	<bean id="sayHelloService" class="com.xx.service.impl.DefaultSayHelloServiceImpl" />
	
	<bean id="sayHelloService_hessianRPC" 	class="org.springframework.remoting.caucho.HessianServiceExporter" >
		<property name="service" 			ref="sayHelloService" />
		<!-- 服务接口 -->
		<property name="serviceInterface" 	value="com.xx.service.ISayHelloService" />
	</bean>
		
	<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="urlMap">
			<map>
				<entry key="/sayHelloService.hessian" 	value-ref="sayHelloService_hessianRPC" />
			</map>
		</property>
	</bean>	
</beans>

二:客户端 调用服务

package com.xx.service;

/**
 * 服务接口 
 */
public interface ISayHelloService {

	/**
	 * @param name
	 * @return
	 */
	 String doSayHello(String name);
	 
	/**
	 * @param name
	 * @param welcomeStr
	 * @return
	 */
	 String doSayHello(String name, String welcomeStr);
}
<?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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.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-2.5.xsd">
	
	<!-- 客户端 -->
	<bean id="sayHelloService"  class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<!-- 重载方法支持 不然会抛出异常  :Caused by: com.caucho.hessian.io.HessianProtocolException:  is an unknown code -->
		<property name="overloadEnabled"	value="true" />
		<property name="serviceUrl" 		value="http://localhost:8080/spring_hessian/sayHelloService.hessian" />
		<!-- 服务接口 -->
		<property name="serviceInterface" 	value="com.xx.service.ISayHelloService"/>
	</bean>
</beans>

 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xx.service.ISayHelloService;

public class ClientMain {
	
	public static void main(String[] args) {
		ISayHelloService sayHelloService = getBean(ISayHelloService.class, "sayHelloService");
		System.out.println(sayHelloService.doSayHello("王五"));
		//客户端必须启用重载方法支持  不然会抛出Caused by: com.caucho.hessian.io.HessianProtocolException: '�' is an unknown code异常
		System.out.println(sayHelloService.doSayHello("李四", "welcome"));
	}
	
	public static <T> T getBean(Class<T> clazs, String beanName) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-client.xml");
		return clazs.cast(applicationContext.getBean(beanName));
	}
}

依赖的jar文件:


 

转载于:https://www.cnblogs.com/java-wl/archive/2012/05/13/2923167.html

相关文章:

  • infoq 七牛云CTO
  • 句柄和ID 指针与handle的区别
  • 运营社群看这篇就够了,微信群门槛设置,用户思维、流量思维与产品思维
  • 解决新建域时提示因密码不合要求面无法创建域
  • C# 如何实现发e-mail
  • SQL SERVER 性能优化三: 索引对数据库的影响
  • 《大道至简 软件工程实践者的思想》 - 书摘精要
  • c27---typedef
  • cURL的发送邮件代码(smtp-multi.c)
  • 20170821
  • ubuntu12.04安装lnmp0.9安装总结
  • C#的一些学习方法
  • 获取用户信息
  • 求职
  • ng2相关内容
  • Angular 4.x 动态创建组件
  • CSS魔法堂:Absolute Positioning就这个样
  • DataBase in Android
  • export和import的用法总结
  • IP路由与转发
  • js写一个简单的选项卡
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • python学习笔记 - ThreadLocal
  • RxJS: 简单入门
  • vue 个人积累(使用工具,组件)
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 如何在 Tornado 中实现 Middleware
  • 收藏好这篇,别再只说“数据劫持”了
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • ​比特币大跌的 2 个原因
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • ###C语言程序设计-----C语言学习(6)#
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (003)SlickEdit Unity的补全
  • (二)斐波那契Fabonacci函数
  • (二)丶RabbitMQ的六大核心
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (一一四)第九章编程练习
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • (转)一些感悟
  • ./configure,make,make install的作用
  • .NET 5种线程安全集合
  • .net wcf memory gates checking failed
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • .NET运行机制
  • @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?
  • @EventListener注解使用说明
  • @RunWith注解作用
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [BZOJ2850]巧克力王国