Spring讓Hessian變得不但強大,而且易用,但是易用背後,卻有不少陷阱!
這個例子很簡單,但實際上的確花費了我超過一小時的時間,排除了種種問題,問題終於水落石出.
整合以上篇Hello Hessian為基礎,加入Spring框架,進行改進.
一、環境
jdk1.5
http://labs.xiaonei.com/apache-mirror/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.zip
http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-2.5.6.SEC01-with-dependencies.zip
順便說下,如果不說環境版本,很難保證你的程序在別的版本下能運行.
二、整合
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已經有894次圍觀