Spring整合Hessian服务

1.Hessian介绍
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

2.创建一个Java工程对外提供一个接口
public interface HessianInterface {

	
	public String say(String str);
}

3.创建一个Web工程,maven中引入上面创建的接口,并实现该接口。
public class HessianInterfaceImpl implements HessianInterface {
	
	@Override
	public String say(String str) {
		return str == null ? null:str;
	}
}

4.相关配置文件的配置
applicationContext.xml:
<bean id="hessianInterfaceImpl" class="com.test.api.impl.HessianInterfaceImpl"></bean>

remote-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName">
           
	
	<import resource="classpath:applicationContext.xml" />
	
	<!--使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
	<bean name="/sayHello" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<!--service为Hession服务接口的实现类的id-->
		<property name="service" ref="hessianInterfaceImpl" />
		<!--service的服务接口-->
		<property name="serviceInterface" value="com.test.api.HessianInterface" />
	</bean>
</beans>

web.xml :
<!-- HESSIAN -->
	<servlet>
		<servlet-name>remote</servlet-name>
		<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>

5.启动容器,Hessian服务URL则为
http://localhost:8080/web工程名/remote/sayHello

6.客户端代码及相关配置
public class HessianClient {
	private HessianInterface sayHello;

	public void sayHello(){
		System.out.println(sayHello.say("Hello world!"));
	}
	
	public HessianInterface getSayHello() {
		return sayHello;
	}

	public void setSayHello(HessianInterface sayHello) {
		this.sayHello = sayHello;
	}
}

applicationContext.xml
<bean id="sayHello" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
	<property name="serviceUrl">
		<value>
			http://192.168.1.101:8067/hessian/remote/sayHello
		</value>
	</property>
	<property name="serviceInterface">
		<value>com.test.api.HessianInterface</value>
	</property>
</bean>
	
<bean id="hessianClient" class="com.test.hessian.HessianClient">
	<property name="sayHello" ref="sayHello"></property>
</bean>

7.编写单元测试
public class HessianClientTest{
	
	@Before
	public void setUp() throws Exception {
		String [] str = {"classpath:applicationContext.xml"};
		cfx = new ClassPathXmlApplicationContext(str);
	}
	
	@Test
	public void test() {
		HessianClient say =  (HessianClient) cfx.getBean("hessianClient");
		say.sayHello();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值