Spring让Hessian变得不但强大,而且易用,但是易用背后,却有不少陷阱!
 
这个例子很简单,但实际上的确花费了我超过一小时的时间,排除了种种问题,最后问题终于水落石出。
 
整合以上篇Hello Hessian为基础,加入Spring框架,进行改进。
 
一、环境
jdk1.5
 
顺便说下,如果不说环境版本,很难保证你的程序在别的版本下能运行。
 
二、整合
 
1、写Spring的发布Hessian服务的配置文件
 
hessian-servlet.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
        <bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
        <bean id="helloService" class="lavasoft.suths.service.HelloService"/> 
        <bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter"> 
                <property name="service" ref="helloService"/> 
                <property name="serviceInterface" value="lavasoft.suths.service.Hello"/> 
        </bean> 
</beans>
 
2、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> 
<web-app 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_2_5.xsd" 
                 version="2.5"> 
        <context-param> 
                <param-name>contextConfigLocation</param-name> 
                <param-value> 
                        /WEB-INF/hessian-servlet.xml 
                </param-value> 
        </context-param> 
        <servlet> 
                <servlet-name>hessian</servlet-name> 
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
                <load-on-startup>4</load-on-startup> 
        </servlet>

<servlet-mapping> 
                <servlet-name>hessian</servlet-name> 
                <url-pattern>/hessian/*</url-pattern> 
        </servlet-mapping> 
</web-app>

 
陷阱:
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 com.caucho.hessian.client.HessianProxyFactory; 
import lavasoft.suths.service.Hello;

import java.net.MalformedURLException;

/** 
* 客户端调用(会依赖服务接口) 

* @author leizhimin 2009-8-14 12:29:33 
*/ 
public class Client { 
        public static void main(String[] args) throws MalformedURLException { 
                String url = "http://localhost:8080/hessianapp/hessian/hello"; 
                HessianProxyFactory factory = new HessianProxyFactory(); 
                Hello hello = (Hello) factory.create(Hello.class, url); 
                System.out.println(hello.sayHello("Hessian")); 
        } 
}

 
运行结果:
Hello Hessian!

Process finished with exit code 0

 
还有一种测试方法,就是在客户端也使用Spring,需要做个配置remoting-client.xml:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
        <bean id="helloServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> 
                <property name="serviceUrl" value="http://localhost:8080/hessianapp/hessian/hello"/> 
                <property name="serviceInterface" value="lavasoft.suths.service.Hello"/> 
        </bean> 
</beans>
 
然后写个测试类:
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

Spring整合Hessian的更多相关文章

  1. Spring整合Hessian访问远程服务

    声明:该文章转载自Spring整合Hessian访问远程服务,本人搬过来只是为了记录下学习Hessian的过程,忘此博主理解,在此感谢,等本人有能力了再学一些原创的东东,本人实践了下,hessianS ...

  2. Spring整合Hessian的使用

    该文章转赞自  https://www.cnblogs.com/ontheroad_lee/p/3797239.htm 个人感觉写的非常好,刚学习,先记录下来 1.1     Hessian简介 He ...

  3. hessian的简单使用以及与spring整合

    Hessian是一个由Caucho Technology开发的轻量级二进制RPC协议.和其他Web服务的实现框架不同的是,Hessian是一个使用二进制格式传输的Web服务协议的框架,相对传统soap ...

  4. Hessian与Spring整合

    1.服务端与Spring的整合 1.1:web.xml中配置控制器 <servlet> <servlet-name>hessian</servlet-name> & ...

  5. spring与hessian整合例

    spring与hessian的简单应用实现例: 开发环境:window7 64,jdk8,tomcat8,spring4.2.5,hessian4.0 开发语言:java hessianServer端 ...

  6. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  7. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  8. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  9. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

随机推荐

  1. MVC视图引擎

    1.视图引擎:把视图解析成浏览器可执行的html代码 2.aspx视图: <%=表达式%>: <% C#代码段 %>: 3.razor视图: @(表达式):@ViewData[ ...

  2. python logging 日志轮转文件不删除问题

    前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...

  3. Objective-C程序结构及语法特点

    程序文件分为头文件(.h)和实现文件(.m): 使用#import关键字将所需的头文件导入程序,并且可以避免程序重复引用相同的头文件: @autoreleasepool { … } 自动释放池: 符号 ...

  4. Guide to Database Migration from Microsoft SQL Server using MySQL Workbench

    http://mysqlworkbench.org/2012/07/migrating-from-ms-sql-server-to-mysql-using-workbench-migration-wi ...

  5. drag drop小游戏

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. 解决mysql中表字符集gbk,列字符集Latin1,python查询乱码问题

    最近在公司碰到一个异常蛋疼的情况,mysql数据库中,数据库和表的字符集都是'gbk',但是列的字符集却是'latin1',于是蛋疼的事情出现了. 无论我连接字符串的`charset`设置为`gbk` ...

  7. 5.0:Spring-bean的加载

    内容来自<Spring深度解析>,之后的不一一复述! 在Spring中,最基本的IOC容器接口是BeanFactory - 这个接口为具体的IOC容器的实现作了最基本的功能规定 - 不管怎 ...

  8. flash链接需要后台调用时的插入flash方法

    <!--<textarea id="syslink" style="display:none;">// 1:打宝塔 2:山寨玩法 3:跨服竞技 ...

  9. NOI2015考试小结

    这次NOI2015有幸获得金牌考进了国家集训队,意味着我的OI退役时间既省选之后有延迟了好几个月,又有了新的目标吧. 先说一下考试之外的感受吧,学军宿舍很牛X,接待NOIers而不提供插座,唯一可以用 ...

  10. Educational Codeforces Round 5 B

    Problem B:http://codeforces.com/contest/616/problem/B B. Dinner with Emma 题意:一对夫妻要去餐厅吃晚饭,Emma 想去最豪华( ...