Hessian入門(與Spring集成)

說明 :

1.    講述如何配置Hessian的服務器端(與Spring集成).

2.    講述客戶端如何調用

①   使用HessianProxyFactory  Hessian代理工廠直接調用

②   使用HessianProxyFactoryBean Hessian代理工廠Bean來完成接口調用.

 

1.    講述如何配置Hessian的服務器端(與Spring集成).

接口定義類: com.wtang.isay. Isay:

 

[java] view plaincopy
  1. package com.wtang.isay;  
  2.   
  3. public interface Isay {  
  4.     public String sayHello(String arg1,String arg2);  
  5. }  

 

接口具體實現類: com.wtang.isay. IsayImpl

[java] view plaincopy
  1. package com.wtang.isay;  
  2.   
  3. public class IsayImpl implements Isay {  
  4.   
  5.     public String sayHello(String arg1, String arg2) {  
  6.         return "Hello:" + arg1 + arg2;  
  7.     }  
  8. }  

配置Web.xml:

[java] view plaincopy
  1. <servlet>  
  2.     <servlet-name>remote</servlet-name>  
  3.     <!-- 使用Spring的代理Servlet -->  
  4.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  5.     <init-param>  
  6.         <param-name>namespace</param-name>  
  7.         <param-value>classes/remote-servlet</param-value>  
  8.     </init-param>  
  9.     <load-on-startup>1</load-on-startup>  
  10. </servlet>  
  11.   
  12. <servlet-mapping>  
  13.     <servlet-name>remote</servlet-name>  
  14.     <url-pattern>/remote/*</url-pattern>  
  15. </servlet-mapping>  

配置remote-servlet.xml[該文件位於src目錄下,即編譯後存在與classes下]:

2.    講述客戶端如何調用

        ① 使用HessianProxyFactory  Hessian代理工廠直接調用

 即:

       

[java] view plaincopy
  1. package com.wtang.test;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import com.caucho.hessian.client.HessianProxyFactory;  
  5. import com.wtang.isay.Isay;  
  6.   
  7. public class NormalClient {  
  8.     public static void main(String[] args) throws MalformedURLException {  
  9.         //Spring Hessian代理Servelet  
  10.         String url = "http://localhost:8080/HessianSpring/remote/helloSpring";  
  11.         HessianProxyFactory factory = new HessianProxyFactory();  
  12.         Isay api = (Isay) factory.create(Isay.class, url);  
  13.   
  14.         System.out.println(api.sayHello("chen""weitang"));  
  15.     }  
  16. }  

 

輸出Hello:chenweitang

 

2.    講述客戶端如何調用

        ② 使用HessianProxyFactoryBean Hessian代理工廠Bean來完成接口調用.

配置客戶端 remote-client.xml:

[java] view plaincopy
  1. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  2. <beans>  
  3.     <!-- 客戶端Hessian代理工廠Bean -->  
  4.     <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
  5.         <!-- 請求代理Servlet路徑 -->          
  6.         <property name="serviceUrl">  
  7. <value>http://localhost:8080/HessianSpring/remote/helloSpring</value>  
  8.         </property>  
  9.         <!-- 接口定義 -->  
  10.         <property name="serviceInterface">  
  11.             <value>com.wtang.isay.Isay</value>  
  12.         </property>  
  13.     </bean>  
  14. </beans>  

 

調用:

[java] view plaincopy
  1. package com.wtang.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import com.wtang.isay.Isay;  
  6.   
  7. public class SpringClient {  
  8.     public static void main(String[] args) {  
  9.         ApplicationContext contex = new ClassPathXmlApplicationContext(  
  10.                 "remote-client.xml");  
  11.   
  12.         // 獲得客戶端的Hessian代理工廠bean  
  13.         Isay i = (Isay) contex.getBean("clientSpring");  
  14.         System.out.println(i.sayHello("chen""weitang"));  
  15.     }  
  16. }  

 

輸出Hello:chenweitang

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章