Spring集成Hessian

来源:互联网 发布:知乎 爬虫 加载 编辑:程序博客网 时间:2024/06/05 09:46

 服务端

        第一步:导入相关包,包括hessian3.1.5.jar和spring-web-3.1.2.RELEASE.jar等。

        第二步:写一个普通的接口,例如以下:

[java] view plain copy
  1. /** 
  2.  * @author Geloin 
  3.  */  
  4. package com.gsoft.geloin.service;  
  5.   
  6. /** 
  7.  * @author Geloin 
  8.  * 
  9.  */  
  10. public interface HelloService {  
  11.   
  12.     public void sayHello(String name);  
  13. }  

        第三步,实现HelloService的接口,如下所示:

[java] view plain copy
  1. /** 
  2.  * @author Geloin 
  3.  */  
  4. package com.gsoft.geloin.service.impl;  
  5.   
  6. import org.springframework.stereotype.Service;  
  7.   
  8. import com.gsoft.geloin.service.HelloService;  
  9.   
  10. /** 
  11.  * @author Geloin 
  12.  * 
  13.  */  
  14. @Service("com.gsoft.geloin.service.HelloService")  
  15. public class HelloServiceImpl implements HelloService {  
  16.   
  17.     /* (non-Javadoc) 
  18.      * @see com.gsoft.geloin.service.HelloService#sayHello(java.lang.String) 
  19.      */  
  20.     @Override  
  21.     public void sayHello(String name) {  
  22.         System.out.println("Hello " + name + "!");  
  23.     }  
  24.   
  25. }  

        第四步,建立对HelloService接口的暴露,假设文件为src/spring/spring-hessian.xml如下所述:

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:lang="http://www.springframework.org/schema/lang"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd               http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">        <!-- Name保持与web.xml中的一致,web.xml下文中描述 -->      <bean name="HelloServiceExporter"          class="org.springframework.remoting.caucho.HessianServiceExporter">          <!-- service的ref与HelloServiceImpl中@Service中配置的一致 -->          <property name="service" ref="com.gsoft.geloin.service.HelloService" />          <!-- 接口的路径 -->          <property name="serviceInterface"              value="com.gsoft.geloin.service.HelloService" />      </bean>  </beans>   


        第五步,配置web.xml,关键配置如下所示:

[java] view plain copy
  1. <!-- 在此处配置刚刚写的spring-hessian.xml的位置 -->  
  2. <context-param>  
  3.     <param-name>contextConfigLocation</param-name>  
  4.     <param-value>    
  5.         classpath:/spring/spring-hessian.xml  
  6.     </param-value>  
  7. </context-param>  
[java] view plain copy
  1. <servlet>  
  2.     <!-- servlet-name保持与spring-hessian.xml中一致 -->  
  3.     <servlet-name>HelloServiceExporter</servlet-name>  
  4.     <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>  
  5. </servlet>  
  6. <servlet-mapping>  
  7.     <servlet-name>HelloServiceExporter</servlet-name>  
  8.     <url-pattern>/HelloService</url-pattern>  
  9. </servlet-mapping>  

        第六步,启动你的项目,假设项目访问地址为http://localhost:8080/hello。



        客户端

        第一步,将服务端中的HelloService打成jar包,放到客户端中,仅接口即可(或可包含相关的bean,以接口中用到的bean为准)。

        第二步,代码如下所示:

[java] view plain copy
  1. /** 
  2.  * @author Geloin 
  3.  */  
  4. package com.gsoft.wcmmoni.manager;  
  5.   
  6. import com.caucho.hessian.client.HessianProxyFactory;  
  7. import com.gsoft.geloin.service.HelloService;  
  8.   
  9. /** 
  10.  * @author Geloin 
  11.  *  
  12.  */  
  13. public class ArticleManager {  
  14.     public static void main(String[] args) {  
  15.         try {  
  16.             String url = "http://localhost:8080/hello/HelloService";  
  17.             HessianProxyFactory factory = new HessianProxyFactory();  
  18.             HelloService helloService = (HelloService) factory.create(  
  19.                     HelloService.class, url);  
  20.             helloService.sayHello("张三");  
  21.         } catch (Exception e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25. }  
0 0
原创粉丝点击