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