Spring整合Hessian
Spring让Hessian变得不但强大,而且易用,但是易用背后,却有不少陷阱!这个例子很简单,但实际上的确花费了我超过一小时的时间,排除了种种问题,最后问题终于水落石出
整合以上篇Hello Hessian为基础,加入Spring框架,进行改进。
一、环境
* jdk1.5
* tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.zip
* spring-framework-2.5.6.SEC01-with-dependencies.zip
顺便说下,如果不说环境版本,很难保证你的程序在别的版本下能运行。
二、整合
1、写Spring的发布Hessian服务的配置文件
hessian-servlet.xml
2、配置web.xml
陷阱:
a)hessian-servlet.xml的文件名必须以<servlet-name>hessian</servlet-name>名字开头,并且加上-servlet.xml一段,组成完整的文件名。
b)hessian-servlet.xml的文件名格式必须是[servlet-name]-servlet.xml格式,否则出错。
三、部署应用
因为涉及到类加载顺序问题,好用IDEA启动Tomcat测试老失败,不知道为啥!这次不用IDEA嵌入式启动Tomcat了,直接自己部署测试。
部署后,启动界面如下:
三、写测试
这次测试,可以在上个例子的基础上进行修改来测试,根据上面的配置,那么请求HelloService的URL应该是:http://localhost:8080/hessianapp/hessian/hello
然后写个测试类:package lavasoft.suths.service.client;import lavasoft.suths.service.Hello;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Spring整合Hessian,客户端测试** @author leizhimin 2009-8-14 15:32:46*/public class TestClient {public static void main(String[] args) {try { ApplicationContext context = new ClassPathXmlApplicationContext("/remoting-client.xml"); Hello hello = (Hello) context.getBean("helloServiceClient"); System.out.println(hello.sayHello("Spring Hession"));}catch (Exception e) { e.printStackTrace();}}}
运行结果:
Hello Spring Hession!
Process finished with exit code 0
陷阱:实际上,看着代码好好,程序有时候还是不能跑,原因是Hessian的版本问题,这里推荐使用Spring自带的版本,就不会有问题了。
整个工程所以依赖的包:
log4j-1.2.15.jar
spring-aop.jar
spring-beans.jar
spring-context.jar
spring-context-support.jar
spring-core.jar
spring-jdbc.jar
spring-jms.jar
spring-orm.jar
spring-test.jar
spring-tx.jar
spring-web.jar
spring-webmvc.jar
spring-webmvc-portlet.jar
spring-webmvc-struts.jar
hessian-3.1.3.jar
aopalliance.jar
commons-logging.jar