Hessian初步使用(配合spring)

首先Hessian是一个非常优秀的框架,仅支持Post请求.

一,准备环境

<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.7</version>
</dependency>

二,代码结构

1,服务端

看官可以简单的做两个接口和实现类,贴出接口,实现类就不写了.

package com.mycode.hessian;

public interface IHello {
	public String sayHello();
}

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:context="http://www.springframework.org/schema/context" 
			  xsi:schemaLocation="
				http://www.springframework.org/schema/beans
				http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
				http://www.springframework.org/schema/context
				http://www.springframework.org/schema/context/spring-context-3.0.xsd">
				
	<!-- 定义普通bean实例 -->
	<bean id="hello" class="com.mycode.hessian.Hello" />
	<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
	<bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<!-- 需要导出的目标bean -->
		<property name="service" ref="hello" />
		<!-- Hessian服务的接口 -->
		<property name="serviceInterface" value="com.mycode.hessian.IHello" />
	</bean>
	
	<!-- ICount接口 -->
	<bean id="count" class="com.mycode.hessian.Count"/>
	<bean name="/count" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="count"/>
		<property name="serviceInterface" value="com.mycode.hessian.ICount" />
	</bean>
</beans>

web.xml

(此处注意一个问题:就是如果不在springMVC的地方配置Init-param,则需要在web-inf下声明一个[servlet-name]-servlet.xml,servlet-name就是

<span style="font-size:18px;">DispatcherServlet的servlet名字)</span>

<!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>Archetype Created Web Application</display-name>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:spring.xml</param-value>
  </context-param>
  
 <listener>
	 <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <servlet>
     <servlet-name>hessian</servlet-name>
     <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <init-param>
     	<param-name>contextConfigLocation</param-name>
     	<param-value>/WEB-INF/hessian.xml</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
   </servlet>
   
   <servlet-mapping>
        <servlet-name>hessian</servlet-name>
        <url-pattern>/*</url-pattern>
   </servlet-mapping>
   
   <!-- Exceptions page -->
   <error-page>
	   	<error-code>405</error-code>
	   	<location>/WEB-INF/405.jsp</location>
   </error-page>
   
      <error-page>
	   	<error-code>404</error-code>
	   	<location>/WEB-INF/404.jsp</location>
   </error-page>
   
      <error-page>
	   	<error-code>500</error-code>
	   	<location>/WEB-INF/500.jsp</location>
   </error-page>
   
      <error-page>
	   	<exception-type>java.lang.Throwable</exception-type>
	   	<location>/WEB-INF/500.jsp</location>
   </error-page>
</web-app>

至此,服务端简单的就配置好了!

2,客户端

   简单:直接将服务端的接口代码复制进客户端工程,只需复制接口,但要保证和服务端的包类结构一致.

   复杂:将接口服务打包放进客户端工程.(maven打jar包)

   结构图:

  


spring.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-3.0.xsd
				http://www.springframework.org/schema/context 
				http://www.springframework.org/schema/context/spring-context-3.0.xsd">
				
	<bean id="helloClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl">
			<value>http://test.bai.com/hello</value>
		</property>
		<property name="serviceInterface">
			<value>com.mycode.hessian.IHello</value>
		</property>
	</bean> 
	
	<bean id="countClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl">
			<value>http://test.bai.com/count</value>
		</property>
		<property name="serviceInterface">
			<value>com.mycode.hessian.IHello</value>
		</property>
	</bean> 
</beans>

HessianClient.java


<span style="font-size:18px;">package com.mycode.client;

import java.net.MalformedURLException;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.caucho.hessian.client.HessianProxyFactory;
import com.mycode.hessian.ICount;
import com.mycode.hessian.IHello;

public class HessianClient {
	private HessianProxyFactory factory;
	
	@Before
	public void init(){
		factory = new HessianProxyFactory();
	}
	
	@Test
	public void testHello() throws MalformedURLException {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		IHello hello = (IHello) context.getBean("helloClient");
		System.out.println(hello.sayHello());
	}
	
	@Test
	public void testCount() throws MalformedURLException{
		String urlName = "http://test.secoo.com/count";
		ICount count = (ICount) factory.create(ICount.class, urlName);
		int re = count.countAdd(2, 5);
		System.out.println(re);
	}
}</span>
2种调用,第一种常用,


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值