1. 程式人生 > >Spring 整合 Hessian

Spring 整合 Hessian

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了,直接自己部署測試。 部署後,啟動介面如下: 三、寫測試 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