spring整合Hessian

  上篇文章简单的介绍了Hessian以及它的一些执行原理,现在我们来看看它与强大框架spring的集成吧!

一、服务端使用spring,我们得下载Hessian支持包和Spring的相应的jar包,可以在我的资源库中进行免费下载:

http://download.csdn.net/detail/harderxin/7129231

1、新建web工程,我取名为HessianSpringServer,在web/WEB-INFO/BIN中导入我们相应的jar包,跟上篇文章一样,编写我们的实体类和接口类、以及接口实现类:

实体用户类,因为该类要通过网络层传输,所以必须实现Serializable接口:

[java]  view plain  copy
  1. package com.server.bean;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class User implements Serializable{  
  6.       
  7.     /** 
  8.      *  
  9.      */  
  10.     private static final long serialVersionUID = 7175134832651443717L;  
  11.     //用户编号  
  12.     private int id;  
  13.     //用户名  
  14.     private String userName;  
  15.     //密码  
  16.     private String password;  
  17.   
  18.     public int getId() {  
  19.         return id;  
  20.     }  
  21.   
  22.     public void setId(int id) {  
  23.         this.id = id;  
  24.     }  
  25.   
  26.     public String getUserName() {  
  27.         return userName;  
  28.     }  
  29.   
  30.     public void setUserName(String userName) {  
  31.         this.userName = userName;  
  32.     }  
  33.   
  34.     public String getPassword() {  
  35.         return password;  
  36.     }  
  37.   
  38.     public void setPassword(String password) {  
  39.         this.password = password;  
  40.     }  
  41.       
  42.     public User(int id, String userName, String password) {  
  43.         super();  
  44.         this.id = id;  
  45.         this.userName = userName;  
  46.         this.password = password;  
  47.     }  
  48.   
  49.     @Override  
  50.     public int hashCode() {  
  51.         final int prime = 31;  
  52.         int result = 1;  
  53.         result = prime * result + id;  
  54.         result = prime * result  
  55.                 + ((password == null) ? 0 : password.hashCode());  
  56.         result = prime * result  
  57.                 + ((userName == null) ? 0 : userName.hashCode());  
  58.         return result;  
  59.     }  
  60.   
  61.     @Override  
  62.     public boolean equals(Object obj) {  
  63.         if (this == obj)  
  64.             return true;  
  65.         if (obj == null)  
  66.             return false;  
  67.         if (getClass() != obj.getClass())  
  68.             return false;  
  69.         User other = (User) obj;  
  70.         if (id != other.id)  
  71.             return false;  
  72.         if (password == null) {  
  73.             if (other.password != null)  
  74.                 return false;  
  75.         } else if (!password.equals(other.password))  
  76.             return false;  
  77.         if (userName == null) {  
  78.             if (other.userName != null)  
  79.                 return false;  
  80.         } else if (!userName.equals(other.userName))  
  81.             return false;  
  82.         return true;  
  83.     }  
  84. }  


定义我们的接口类:

[java]  view plain  copy
  1. package com.server.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.server.bean.User;  
  6.   
  7. public interface UserService {  
  8.       
  9.     public List<User> getUser();  
  10.       
  11. }  

定义我们的接口实现类:

[java]  view plain  copy
  1. package com.server.service.impl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.server.bean.User;  
  7. import com.server.service.UserService;  
  8.   
  9. public class UserServiceImpl implements UserService{  
  10.   
  11.     public List<User> getUser() {  
  12.         //我们可以在这个方法中与数据库打交道  
  13.         List<User> list=new ArrayList<User>();  
  14.         list.add(new User(1,"Mary","123456"));  
  15.         list.add(new User(2,"Jack","236547"));  
  16.         list.add(new User(3,"Joy","362541"));  
  17.         return list;  
  18.     }  
  19. }  


2、新建spring配置文件springremoting-servlet.xml,让接口及实现类由spring容器去管理:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="  
  3.         http://www.springframework.org/schema/beans  
  4.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  5.         http://www.springframework.org/schema/context  
  6.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  7.           
  8.         <!-- Hessian服务接口Impl注入 -->  
  9.         <bean id="userService" class="com.server.service.impl.UserServiceImpl"/>  
  10.           
  11.         <!-- 使用HessianServiceExporter为服务接口Impl在网络地址中映射一个Hessian服务-->  
  12.     <!-- 完整的远程调用请求<a href="http://localhost:8080/HessianSpringServer/sr/userService" target="_blank">Url:http://localhost:8080/HessianSpringServer/sr/userService</a>,前部分在web.xml中已经进行了配置 -->  
  13.         <bean name="/userService" class="org.springframework.remoting.caucho.HessianServiceExporter">  
  14.             <!-- Hessian服务的接口 -->  
  15.             <property name="serviceInterface" value="com.server.service.UserService"/>  
  16.             <!-- Hessian服务的接口Impl -->  
  17.             <property name="service" ref="userService"></property>  
  18.         </bean>  
  19. </beans>  


3、配置我们的web.xml文件,让服务器启动后能够加载到我们的springremoting-servlet.xml,以及配置访问路径

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.     <servlet>  
  9.         <!-- 完整的远程调用请求Url:http://localhost:8080/HessianSpringServer/sr/* -->  
  10.         <servlet-name>springremoting</servlet-name>  
  11.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  12.         <init-param>  
  13.             <param-name>contextConfigLocation</param-name>  
  14.             <!-- 服务启动加载 springremoting-servlet.xml-->  
  15.             <param-value>classpath:springremoting-servlet.xml</param-value>  
  16.         </init-param>  
  17.         <load-on-startup>1</load-on-startup>  
  18.     </servlet>  
  19.     <servlet-mapping>  
  20.         <servlet-name>springremoting</servlet-name>  
  21.         <url-pattern>/sr/*</url-pattern>  
  22.     </servlet-mapping>  
  23.     <welcome-file-list>  
  24.         <welcome-file>index.jsp</welcome-file>  
  25.     </welcome-file-list>  
  26. </web-app>  

好了,我们的简单的服务端搭建完成了,记得把我们的接口类(UserService)和实体类(User)打成相应的jar包(也可以在客户端复制服务端接口类的代码,这样太繁琐了),因为我们的客户端需要访问到,这是Hessian里面定义的,No Why!

将我们的项目部署到Tomcat服务器上,启动Tomcat,如果报错了,说明你的配置没有配好哦,得自己仔细检查检查,如果不报错,说明我们的服务端搭建完成,下面来编写我们的客户端吧!

二、客户端不使用Spring,新建java项目,导入我们的Hessian支持包和在服务端打包的借口类jar包

编写我们的测试类,因为我把没有spring和有spring的客户端测试类写在了一起,好做个对比:

[java]  view plain  copy
  1. package com.client.test;  
  2.   
  3.   
  4. import java.util.List;  
  5.   
  6. import com.caucho.hessian.client.HessianProxyFactory;  
  7. import com.server.bean.User;  
  8. import com.server.service.UserService;  
  9.   
  10. public class UserServiceTest {  
  11.     public static void main(String[] args) {  
  12.         //不使用Spring服务器访问地址  
  13.         //String url="http://localhost:8080/HessianServer/us";  
  14.         //使用spring服务器访问的地址  
  15.         String url="http://localhost:8080/HessianSpringServer/sr/userService";  
  16.         //获得HessianProxyFactory实例  
  17.         HessianProxyFactory factory=new HessianProxyFactory();  
  18.         try {  
  19.             //不使用Spring创建我们的接口对象  
  20.             //UserService userService=(UserService)factory.create(url);  
  21.             //使用Spring创建我们的接口对象  
  22.             UserService userService=(UserService)factory.create(UserService.class,url);  
  23.             //执行服务端方法  
  24.             List<User> users=userService.getUser();  
  25.             //遍历输出  
  26.             for(User user:users){  
  27.                 System.out.println("id="+user.getId()+",name="+user.getUserName()+",pwd="+user.getPassword());  
  28.             }  
  29.         } catch (Exception e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33. }  

其实我在做测试的时候还是有一个疑问的,就是我发现在服务端Hessian和spring整合后,客户端需要这样去调用:factory.create(UserService.class,url);需要指定相应接口的class,不能直接使用url地址传入:factory.create(url);这样会抛出一个异常:UndeclaredThrowableException,其实服务端在spring配置文件里面已经给接口类以及实现类进行了注入:

[html]  view plain  copy
  1. <!-- Hessian服务接口Impl注入 -->  
  2.     <bean id="userService" class="com.server.service.impl.UserServiceImpl"/>  
  3.       
  4.     <!-- 使用HessianServiceExporter为服务接口Impl在网络地址中映射一个Hessian服务-->  
  5. t;!-- 完整的远程调用请求Url:http://localhost:8080/HessianSpringServer/sr/userService -->  
  6.     <bean name="/userService" class="org.springframework.remoting.caucho.HessianServiceExporter">  
  7.         <!-- Hessian服务的接口 -->  
  8.         <property name="serviceInterface" value="com.server.service.UserService"/>  
  9.         <!-- Hessian服务的接口Impl -->  
  10.         <property name="service" ref="userService"></property>  
  11.     </bean>  


并且,没生成一个Service,都会要重新写一个bean,所以这里我觉得直接使用地址factory.create(url);是可以访问的,跟没有使用spring时候在web.xml中配置一样,可是它不行,应该是spring里面给它做处理了吧,我们必须要factory.create(UserService.class,url);不然,里面会找不到相应的实体类吧!大家在写代码的时候注意下就是了!

运行main函数,得到的结果为:

id=1,name=Mary,pwd=123456
id=2,name=Jack,pwd=236547
id=3,name=Joy,pwd=362541

测试成功,哈哈...!

三、客户端使用Spring,我们的客户端也可以使用Spring,让factory.create(UserService.class,url);交给spring容器去处理,同样,需要导入我们的Hessian支持包和在服务端打包的借口类jar包,还需要我们的spring支持包,上面已经给出了相应的下载地址,如果你客户端需要使用JUnit进行测试,也需要junit的测试类包,我的那个包文件里面都已经包含了

1、既然我们在客户端使用了spring,当然得编写我们的配置文件,例如springremoting-client.xml,然后将我们的访问地址和接口类在里面进行注入:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="  
  3.         http://www.springframework.org/schema/beans  
  4.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  5.         http://www.springframework.org/schema/context  
  6.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  7.           
  8.         <bean id="userService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
  9.             <!-- 注入我们的接口类 -->  
  10.             <property name="serviceInterface" value="com.server.service.UserService"/>  
  11.             <!-- 服务器访问地址 -->  
  12.             <property name="serviceUrl" value="http://localhost:8080/HessianSpringServer/sr/userService"/>  
  13.         </bean>  
  14. </beans>  

2、编写我们的测试函数:

[html]  view plain  copy
  1. package com.client.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import com.server.bean.User;  
  9. import com.server.service.UserService;  
  10.   
  11. public class UserServiceSpringTest {  
  12.     public static void main(String[] args) {  
  13.         //加载我们的Spring配置文件  
  14.         ApplicationContext context=new ClassPathXmlApplicationContext("springremoting-client.xml");  
  15.         //获取我们的接口类  
  16.         UserService userService=(UserService)context.getBean("userService");  
  17.         List<User> users=userService.getUser();  
  18.         //遍历输出  
  19.         for(User user:users){  
  20.             System.out.println("id="+user.getId()+",name="+user.getUserName()+",pwd="+user.getPassword());  
  21.         }  
  22.     }  
  23. }  

测试输出:

id=1,name=Mary,pwd=123456
id=2,name=Jack,pwd=236547
id=3,name=Joy,pwd=362541
两种情况的不同之处在于客户端里面的Hessian工厂对象交给spring容器去管理了,各有各的好处,大家可以根据实际情况进行选择!

使用junit测试我们的客户端:

[java]  view plain  copy
  1. /** 
  2.  * 
  3.  * Copyright: Copyright (c) 2012 Asiainfo-Linkage 
  4.  * 
  5.  * client@date:2014-3-28 
  6.  * @ClassName: UserServiceTest.java 
  7.  * @Description: 该类的功能描述 
  8.  * 
  9.  * @version: v1.0.0上午9:01:27eleven 
  10.  * @author: elevenHessianSpringClient 
  11.  * 
  12.  * Modification History: 
  13.  * Date  Author  Version Description 
  14.  * ---------------------------------------------------------* 
  15.  * 2014-3-28 eleven v1.0.0 新建 
  16.  */  
  17. package client;  
  18.   
  19.   
  20. import javax.annotation.Resource;  
  21.   
  22. import org.junit.Test;  
  23. import org.junit.runner.RunWith;  
  24. import org.springframework.test.context.ContextConfiguration;  
  25. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  26.   
  27. import service.UserServiceI;  
  28.   
  29. /** 
  30.  * @Title: UserServiceTest.java 
  31.  * @Description: TODO(这里用一句话描述这个类的作用) 
  32.  * @author eleven 
  33.  * @date 2014-3-28 上午9:01:27 
  34.  * 
  35.  */  
  36. @RunWith(SpringJUnit4ClassRunner.class)  
  37. @ContextConfiguration(locations = {"classpath:springremoting-client.xml"})  
  38. public class UserServiceTest {  
  39.       
  40.     private UserServiceI userService;  
  41.       
  42.     @Resource  
  43.     public void setUserService(UserServiceI userService) {  
  44.         this.userService = userService;  
  45.     }  
  46.   
  47.     @Test  
  48.     public void test() {  
  49.         System.out.println(userService.queryUserList());  
  50.     }  
  51.   
  52. }  


好了,Hessian与Spring整合完成,希望大家一起交流学习,共同进步!Hessian与sping、struts、hibernate三大框架整合,大家可以去尝试一下哦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于计算机专业的学生而言,参加各类比赛能够带来多方面的益处,具体包括但不限于以下几点: 技能提升: 参与比赛促使学生深入学习和掌握计算机领域的专业知识与技能,如编程语言、算法设计、软件工程、网络安全等。 比赛通常涉及实际问题的解决,有助于将理论知识应用于实践中,增强问题解决能力。 实践经验: 大多数比赛都要求参赛者设计并实现解决方案,这提供了宝贵的动手操作机会,有助于积累项目经验。 实践经验对于计算机专业的学生尤为重要,因为雇主往往更青睐有实际项目背景的候选人。 团队合作: 许多比赛鼓励团队协作,这有助于培养学生的团队精神、沟通技巧和领导能力。 团队合作还能促进学生之间的知识共享和思维碰撞,有助于形成更全面的解决方案。 职业发展: 获奖经历可以显著增强简历的吸引力,为求职或继续深造提供有力支持。 某些比赛可能直接与企业合作,提供实习、工作机会或奖学金,为学生的职业生涯打开更多门路。 网络拓展: 比赛是结识同行业人才的好机会,可以帮助学生建立行业联系,这对于未来的职业发展非常重要。 奖金与荣誉: 许多比赛提供奖金或奖品,这不仅能给予学生经济上的奖励,还能增强其成就感和自信心。 荣誉证书或奖状可以证明学生的成就,对个人品牌建设有积极作用。 创新与研究: 参加比赛可以激发学生的创新思维,推动科研项目的开展,有时甚至能促成学术论文的发表。 个人成长: 在准备和参加比赛的过程中,学生将面临压力与挑战,这有助于培养良好的心理素质和抗压能力。 自我挑战和克服困难的经历对个人成长有着深远的影响。 综上所述,参加计算机领域的比赛对于学生来说是一个全面发展的平台,不仅可以提升专业技能,还能增强团队协作、沟通、解决问题的能力,并为未来的职业生涯奠定坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值