国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
用spirng和hessian構(gòu)建分布式應(yīng)用(遠(yuǎn)程接口)的方法[原] - Spring -...
最近一期的《programmer》里幾乎從頭至尾在講關(guān)于“J2EE without EJB”的事情,可憐的ejb啊,居然被描述成了遺產(chǎn)系統(tǒng)的重要組成部分。。。

其實(shí)有上面的結(jié)論,無外乎現(xiàn)在java里面的新技術(shù)已經(jīng)幾乎能完全取代ejb的優(yōu)點(diǎn),而克服ejb的缺點(diǎn),entity bean和有狀態(tài)的session bean已經(jīng)機(jī)乎被視為垃圾,hibernate和spring大行其到,看看最進(jìn)n期《programmer》中篇幅的比重就知道了。本來我個人的感覺是hibernate取代了entity bean,spring取代了session bean,但是ejb的遠(yuǎn)程調(diào)用用hibernate和spring的架構(gòu)還取代不了,可是在最近的一期《programmer》中我發(fā)現(xiàn)了Hessian!更爽的是,發(fā)現(xiàn)了spring原來可以和Hessian結(jié)合使用!看來真的可以say byebye to ejb了。

看到這么振奮人心的消息,怎么能不親自試驗(yàn)一下呢,于是上http://www.caucho.com/以迅雷不及掩耳盜鈴之勢下載了Hessian的src jar和bin jar。create一個工程,把這些相關(guān)的jar統(tǒng)統(tǒng)扔進(jìn)去,配置和coding就可以開始了。首先,開了如下幾個包:



whao.test.hessian.server
放遠(yuǎn)程服務(wù)的接口

whao.test.hessian.server.impl
放遠(yuǎn)程服務(wù)的實(shí)現(xiàn)類

whao.test.hessian.client
放客戶端應(yīng)用



1. whao.test.hessian.server中寫一個MyService接口:
Java代碼
  1. /*  
  2.  
  3.  * Created on 2005-7-25  
  4.  
  5.  *  
  6.  
  7.  */  
  8.   
  9. package whao.test.hessian.server;   
  10.   
  11.     
  12.   
  13. /**  
  14.  
  15.  * @author Hao Wei  
  16.  
  17.  *  
  18.  
  19.  */  
  20.   
  21. public interface MyService {   
  22.   
  23.     public String doSomething(String s);;   
  24.   
  25. }  



2. whao.test.hessian.server.impl中寫一個實(shí)現(xiàn)類

Java代碼
  1. /*  
  2.  
  3.  * Created on 2005-7-25  
  4.  
  5.  *  
  6.  
  7.  */  
  8.   
  9. package whao.test.hessian.server.impl;   
  10.   
  11.     
  12.   
  13. import whao.test.hessian.server.MyService;   
  14.   
  15.     
  16.   
  17. /**  
  18.  
  19.  * @author Hao Wei  
  20.  
  21.  *  
  22.  
  23.  */  
  24.   
  25. public class MyServiceImpl implements MyService {   
  26.   
  27.     
  28.   
  29.     /* (non-Javadoc);  
  30.  
  31.      * @see whao.test.hessian.server.MyService#doSomething(java.lang.String);  
  32.  
  33.      */  
  34.   
  35.     public String doSomething(String s); {   
  36.   
  37.         return "HAHAHA: " + s;   
  38.   
  39.     }   
  40.   
  41. }   
  42.   
  43.    


3. 配置遠(yuǎn)程服務(wù)

         Hessian的遠(yuǎn)程服務(wù)要配置成servlet,配置如下:
Java代碼
  1. <servlet>   
  2.   
  3.    <servlet-name>myservice</servlet-name>   
  4.   
  5.    <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>   
  6.   
  7.    <load-on-startup>1</load-on-startup>   
  8.   
  9.    <init-param>   
  10.   
  11.        <param-name>service-class</param-name>   
  12.   
  13.        <param-value>whao.test.hessian.server.impl.MyServiceImpl</param-value>   
  14.   
  15.    </init-param>   
  16.   
  17. </servlet>   
  18.   
  19. <servlet-mapping>   
  20.   
  21.    <servlet-name>myservice</servlet-name>   
  22.   
  23.    <url-pattern>/myservice</url-pattern>   
  24.   
  25. </servlet-mapping>   


這樣,當(dāng)啟動了這個web應(yīng)用,這個遠(yuǎn)程服務(wù)就以url http://localhost:8080/test_web/myservice 的形式發(fā)布了。然后就是開發(fā)客戶端來調(diào)用這個遠(yuǎn)程服務(wù)。

Java代碼
  1. /*  
  2.  
  3.  * Created on 2005-7-25  
  4.  
  5.  *  
  6.  
  7.  */  
  8.   
  9. package whao.test.hessian.client;   
  10.   
  11.     
  12.   
  13. import whao.test.hessian.server.MyService;   
  14.   
  15.     
  16.   
  17. import com.caucho.hessian.client.HessianProxyFactory;   
  18.   
  19.     
  20.   
  21. /**  
  22.  
  23.  * @author Hao Wei  
  24.  
  25.  *    
  26.  
  27.  */  
  28.   
  29. public class TestMain {   
  30.   
  31.     public static void main(String[] args); throws Exception {   
  32.   
  33.         HessianProxyFactory proxyFactory = new HessianProxyFactory();;   
  34.   
  35.         MyService service = (MyService); proxyFactory.create(MyService.class,   
  36.   
  37.                 "http://localhost:8080/test_web/myservice");;   
  38.   
  39.         System.out.println(service.doSomething("xixixixi"););;   
  40.   
  41.         System.out.println("ok!");;   
  42.   
  43.     }   
  44.   
  45. }   
  46.   
  47.    


運(yùn)行一把,顯示

HAHAHA:xixixi

ok!

不錯不錯,純Hessian的遠(yuǎn)程調(diào)用就這樣搞定了。繼續(xù)研究《programmer》看到上面介紹用spring的遠(yuǎn)程訪問解決方案來訪問ejb的遠(yuǎn)程服務(wù)。什么?spring還有遠(yuǎn)程解決方案?沒聽說過嘛,看了《programmer》上的介紹,發(fā)現(xiàn)是真的。那么既然spring能訪問ejb的遠(yuǎn)程服務(wù),那么能訪問Hessian的遠(yuǎn)程服務(wù)么?打開spring.jar看看,居然發(fā)現(xiàn)了名曰org.springframework.remoting.caucho.HessianProxyFactoryBean的類!夏昕真不厚道啊,再他的spring中文教程中居然匿掉了spring里這么好的東西!于是打開sping英文版reference,終于找到了用spring配置Hessian客戶端的方法:
Java代碼
  1. <bean id="myService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">   
  2.   
  3.    <property name="serviceUrl">   
  4.   
  5.        <value>   
  6.   
  7.        http://localhost:8080/test_web/myservice   
  8.   
  9.        </value>   
  10.   
  11.    </property>   
  12.   
  13.    <property name="serviceInterface">   
  14.   
  15.        <value>whao.test.hessian.server.MyService</value>   
  16.   
  17.    </property>   
  18.   
  19. </bean>   

然后客戶端就可以這么玩啦:

Java代碼
  1. /*  
  2.  
  3.  * Created on 2005-7-25  
  4.  
  5.  *  
  6.  
  7.  */  
  8.   
  9. package whao.test.hessian.client;   
  10.   
  11.     
  12.   
  13. import whao.test.hessian.server.MyService;   
  14.   
  15. import whao.util.spirng.SpringBeanFactory;   
  16.   
  17.     
  18.   
  19. import com.caucho.hessian.client.HessianProxyFactory;   
  20.   
  21.     
  22.   
  23. /**  
  24.  
  25.  * @author Hao Wei  
  26.  
  27.  *    
  28.  
  29.  */  
  30.   
  31. public class TestMain {   
  32.   
  33.     
  34.   
  35.     public static void main(String[] args); throws Exception {   
  36.   
  37.         testWithSpring();;   
  38.   
  39.     }   
  40.   
  41.     public static void testWithSpring();{   
  42.   
  43.         MyService service = (MyService);SpringBeanFactory.getBean("myService");;   
  44.   
  45.         System.out.println(service.doSomething("lllllllll"););;   
  46.   
  47.         System.out.println("ok!");;   
  48.   
  49.     }   
  50.   
  51.     public static void testWithoutSpring(); throws Exception {   
  52.   
  53.         HessianProxyFactory proxyFactory = new HessianProxyFactory();;   
  54.   
  55.         MyService service = (MyService); proxyFactory.create(MyService.class,   
  56.   
  57.                 "http://localhost:8080/test_web/myservice");;   
  58.   
  59.         System.out.println(service.doSomething("xixixixi"););;   
  60.   
  61.         System.out.println("ok!");;   
  62.   
  63.     }   
  64.   
  65. }   
  66.   
  67.    


執(zhí)行一下,輸出是:

HAHAHA:lllllllll

ok!

spring真是個好東東,呵呵,其中的SpringBeanFactory是這樣實(shí)現(xiàn)的:
Java代碼
  1.   
  2. /*  
  3.  
  4.  * Created on 2005-7-25  
  5.  
  6.  *  
  7.  
  8.  */  
  9.   
  10. package whao.util.spirng;   
  11.   
  12.     
  13.   
  14. import javax.servlet.ServletContext;   
  15.   
  16.     
  17.   
  18. import org.apache.commons.logging.Log;   
  19.   
  20. import org.apache.commons.logging.LogFactory;   
  21.   
  22. import org.springframework.context.ApplicationContext;   
  23.   
  24. import org.springframework.context.support.FileSystemXmlApplicationContext;   
  25.   
  26. import org.springframework.web.context.support.WebApplicationContextUtils;   
  27.   
  28.     
  29.   
  30. /**  
  31.  
  32.  * @author Hao Wei  
  33.  
  34.  *  
  35.  
  36.  */  
  37.   
  38. public class SpringBeanFactory {   
  39.   
  40.     private static final Log log = LogFactory.getLog(SpringBeanFactory.class);;   
  41.   
  42.          private static ApplicationContext ctx = null;   
  43.   
  44.     
  45.   
  46.     public static Object getBean(ServletContext context, String beanID); {   
  47.   
  48.         log.info("beanID=" + beanID);;   
  49.   
  50.         ApplicationContext ac = WebApplicationContextUtils   
  51.   
  52.                 .getWebApplicationContext(context);;   
  53.   
  54.         return ac.getBean(beanID);;   
  55.   
  56.     }   
  57.   
  58.        
  59.   
  60.     public static Object getBean(String beanID);{   
  61.   
  62.                    if(ctx == null);{   
  63.   
  64.                             ctx = new FileSystemXmlApplicationContext(   
  65.   
  66.                             "D:\\whao-work\\src\\test_web\\test_web\\WEB-INF\\applicationContext.xml");;   
  67.   
  68.                    }   
  69.   
  70.                    return ctx.getBean(beanID);;   
  71.   
  72.     }   
  73.   
  74. }   
  75.   
  76.    


看到sping.jar的包org.springframework.remoting.caucho中還有另外兩個類叫HessianClientInterceptor和HessianServiceExporter,看來貌似hessian服務(wù)端也可以和spring結(jié)合,還有客戶端的Interceptor估計(jì)可以實(shí)現(xiàn)對遠(yuǎn)程服務(wù)的AOP,要繼續(xù)研究一下了,待續(xù)吧
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Hessian學(xué)習(xí)筆記
簡單之美 | 基于Dubbo的Hessian協(xié)議實(shí)現(xiàn)遠(yuǎn)程調(diào)用
普通Java類獲取Spring的bean
springmvc攔截404錯誤
spring和hessian整合
hessian教程
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服