熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring整合Hessian

2022-06-13   來源: Java開源技術 

  Spring讓Hessian變得不但強大而且易用但是易用背後卻有不少陷阱!

  這個例子很簡單但實際上的確花費了我超過一小時的時間排除了種種問題最後問題終於水落石出

  整合以上篇Hello Hessian為基礎加入Spring框架進行改進

  環境

  jdk

  mirror/tomcat/tomcat/v/bin/apachetomcatzip

  frameworkSECwithdependencieszip

  順便說下如果不說環境版本很難保證你的程序在別的版本下能運行

  整合

  寫Spring的發布Hessian服務的配置文件

  hessianservletxml

   <!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>
<beans>
        <bean id=defaultHandlerMapping class=orgspringframeworkwebservlethandlerBeanNameUrlHandlerMapping/>
        <bean id=helloService class=lavasoftsuthsserviceHelloService/>
        <bean name=/hello class=orgspringframeworkremotingcauchoHessianServiceExporter>
                <property name=service ref=helloService/>
                <property name=serviceInterface value=lavasoftsuthsserviceHello/>
        </bean>
</beans>

  配置webxml

   <?xml version= encoding=UTF?>
<webapp xmlns=
                 xmlns:xsi=instance
                 xsi:schemaLocation=
        app__xsd
                 version=>
        <contextparam>
                <paramname>contextConfigLocation</paramname>
                <paramvalue>
                        /WEBINF/hessianservletxml
                </paramvalue>
        </contextparam>
        <servlet>
                <servletname>hessian</servletname>
                <servletclass>orgspringframeworkwebservletDispatcherServlet</servletclass>
                <loadonstartup></loadonstartup>
        </servlet>

        <servletmapping>
                <servletname>hessian</servletname>
                <urlpattern>/hessian/*</urlpattern>
        </servletmapping>
</webapp>

  陷阱

  a)hessianservletxml的文件名必須以<servletname>hessian</servletname>名字開頭並且加上servletxml一段組成完整的文件名

  b)hessianservletxml的文件名格式必須是[servletname]servletxml格式否則出錯

  部署應用

  因為涉及到類加載順序問題好用IDEA啟動Tomcat測試老失敗不知道為啥!這次不用IDEA嵌入式啟動Tomcat了直接自己部署測試

  部署後啟動界面如下

  

  寫測試

  這次測試可以在上個例子的基礎上進行修改來測試根據上面的配置那麼請求HelloService的URL應該是//localhost/hessianapp/hessian/hello

   package lavasoftsuthsserviceclient;

import comcauchohessianclientHessianProxyFactory;
import lavasoftsuthsserviceHello;

import MalformedURLException;

/**
* 客戶端調用(會依賴服務接口)
*
* @author leizhimin ::
*/
public class Client {
        public static void main(String[] args) throws MalformedURLException {
                String url = //localhost:/hessianapp/hessian/hello;
                HessianProxyFactory factory = new HessianProxyFactory();
                Hello hello = (Hello) factorycreate(Helloclass url);
                Systemoutprintln(hellosayHello(Hessian));
        }
}

  運行結果

   Hello Hessian!

Process finished with exit code

  還有一種測試方法就是在客戶端也使用Spring需要做個配置remotingclientxml

   <!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>
<beans>
        <bean id=helloServiceClient class=orgspringframeworkremotingcauchoHessianProxyFactoryBean>
                <property name=serviceUrl value=//localhost:/hessianapp/hessian/hello/>
                <property name=serviceInterface value=lavasoftsuthsserviceHello/>
        </bean>
</beans>

  然後寫個測試類

   package lavasoftsuthsserviceclient;

import lavasoftsuthsserviceHello;
import orgntextApplicationContext;
import orgntextsupportClassPathXmlApplicationContext;

/**
* Spring整合Hessian客戶端測試
*
* @author leizhimin ::
*/
public class TestClient {
        public static void main(String[] args) {
                try {
                        ApplicationContext context = new ClassPathXmlApplicationContext(/remotingclientxml);
                        Hello hello = (Hello) contextgetBean(helloServiceClient);
                        Systemoutprintln(hellosayHello(Spring Hession));
                }
                catch (Exception e) {
                        eprintStackTrace();
                }
        }
}

  運行結果

   Hello Spring Hession!

Process finished with exit code

  陷阱實際上看著代碼好好程序有時候還是不能跑原因是Hessian的版本問題這裡推薦使用Spring自帶的版本就不會有問題了

  整個工程所以依賴的包

   logjjar
springaopjar
springbeansjar
springcontextjar
springcontextsupportjar
springcorejar
springjdbcjar
springjmsjar
springormjar
springtestjar
springtxjar
springwebjar
springwebmvcjar
springwebmvcportletjar
springwebmvcstrutsjar
hessianjar
aopalliancejar
commonsloggingjar


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28360.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.