Spring实例

  之前,我们做了很简单的纯Hessian的调用,虽然到此已经能够满足远程调用的需求了,但是我听说spring也能够访问hessian的远程服务,研究了一番,废话不多说,直接上示例。

业务场景

  servlet的例子并未涉及到复杂对象的传输,这次我们搞复杂点,设计一个服务,通过远程调用的方式来找爸爸的儿子。

服务端

环境搭建

 引入hessian、spring-mvc的相关jar包,后面会附上相关的pom文件配置,项目结构如下:

示例代码

  复杂的对象传输时,只需要类继承Serializable,保证在数据传输时能够序列化和反序列化,如下面的Father和Child类。

父亲:

 package example;

 import java.io.Serializable;

 /**
  * @author X
  */
 public class Father implements Serializable {

     private static final long serialVersionUID = 1L;

     public Father(String name) {
         this.name = name;
     }

     private String name;

     public String getName() {
         return name;
     }
 }

Father.java

儿子:

 package example;

 import java.io.Serializable;

 /**
  * @author X
  */
 public class Child implements Serializable {

     private static final long serialVersionUID = 1L;

     public Child(String name) {
         this.name = name;
     }

     private String name;

     public String getName() {
         return name;
     }
 }

Child.java

接送接口:

 package example;

 /**
  * @author X
  */
 public interface ShuttleService {
     String getCar();

     Child getChild(Father father);
 }

ShuttleService.java

接送实现:

 package example;

 /**
  * @author X
  */
 public class ShuttleServiceImpl implements ShuttleService {

     public String getCar() {
         return "小火车";
     }

     public Child getChild(Father father) {
         if (father != null)
             return new Child(father.getName() + "的儿子");
         return null;
     }
 }

ShuttleServiceImpl.java

spring配置:

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean id="shuttle" class="example.ShuttleServiceImpl"/>
     <bean name="/shuttleService" class="org.springframework.remoting.caucho.HessianServiceExporter">
         <property name="service">
             <ref bean="shuttle"/>
         </property>
         <property name="serviceInterface">
             <value>example.ShuttleService</value>
         </property>
     </bean>
 </beans>

hessian-spring.xml

web配置:

 <!DOCTYPE web-app PUBLIC
         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
         "http://java.sun.com/dtd/web-app_2_3.dtd" >

 <web-app>
     <display-name>Demo</display-name>
     <servlet>
         <servlet-name>shuttle</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/hessian-spring.xml</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>shuttle</servlet-name>
         <url-pattern>/rpc/*</url-pattern>
     </servlet-mapping>
 </web-app>

web.xml

依赖配置:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     <parent>
         <artifactId>container</artifactId>
         <groupId>hessian.host</groupId>
         <version>1.0-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>spring</artifactId>
     <packaging>war</packaging>
     <name>spring Maven Webapp</name>
     <url>http://maven.apache.org</url>
     <dependencies>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>3.8.1</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>com.caucho</groupId>
             <artifactId>hessian</artifactId>
             <version>4.0.7</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
             <version>3.0.6.RELEASE</version>
         </dependency>
     </dependencies>
     <build>
         <finalName>spring</finalName>
     </build>

pom.xml

客户端

环境搭建

  引入hessian、spring-mvc的相关jar包,项目结构如下:

调用:

 import example.Father;
 import example.ShuttleService;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;

 import java.net.MalformedURLException;

 /**
  * @author X
  */
 public class Run {
     public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {
         ApplicationContext context = new ClassPathXmlApplicationContext("spring-rpc.xml");
         ShuttleService ss = (ShuttleService) context.getBean("rpcClient");
         System.out.println(ss.getCar());
         System.out.println(ss.getChild(new Father("王老二")).getName());
     }
 }

Run.java

客户端spring配置:

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean id="rpcClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
         <property name="serviceUrl">
             <value>http://localhost:45/rpc/shuttleService</value>
         </property>
         <property name="serviceInterface">
             <value>example.ShuttleService</value>
         </property>
     </bean>
 </beans>

spring-rpc.xml

运行后输出结果如下:

  

  注意在实例中我是把Father和Child和ShuttleService分别定义了两次,实际开发中不建议这样做,把需要调用的部分作为一个公共API供分别提供给客户端和服务端使用,否则可能会照成反序列化失败。

  把spring和hessian相结合后,无论是服务端还是客户端的业务代码中,已经没有一行与hessian相关的代码了,也就是说spring让我们的开发无关远程接口的实现了,这样我们就可只关注于开发而不必去关注远程调用怎么去实现。如果说我们哪天需要切换另外一个spring支持的远程访问接口,也只需要修改下配置文件就搞定了,so easy,妈妈再也不用担心我的实现代码了!

最新文章

  1. mac系统安装node
  2. MyEclipse 序列号生成代码
  3. apache+tomcat分布式集群搭建
  4. java 内存分析
  5. Spring事务配置的五种方式 -- 越往后需要Spring版本越高
  6. BAT命令介绍【转自Internet】
  7. swift和oc区别----属性部分(参考官方swift2.1文档)
  8. Eclipse-----jrebel实现jetty热部署
  9. linux tcp中time_wait
  10. css3新属性的学习使用
  11. c++学习笔记---03---从一个小程序说起2
  12. 【技巧】Java工程中的Debug信息分级输出接口
  13. arcEngine开发之查询的相关接口
  14. 爬虫之scrapy--基本操作
  15. 用word发布CSDN文章
  16. JS--变量及深浅拷贝
  17. node api 之:util
  18. IBM主机家族——大型机、中型机、小型机
  19. 静态构造器(static constructor)
  20. centOS下升级python版本,详细步骤

热门文章

  1. MySQL 5.6 Reference Manual-14.6 InnoDB Table Management
  2. swift可选值总结
  3. codeforces 468B two set(并查集)
  4. Django01 web http 基础
  5. Redmine 甘特图导出 PDF 和 PNG 中文乱码问题
  6. 洛谷P3195 [HNOI2008]玩具装箱TOY 斜率优化
  7. VS2008集成QT的OpenGL开发(实现二维图形的旋转)
  8. P1892 团伙
  9. P1265 公路修建 (prim)
  10. strtotime的一个使用问题