qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Java学习之路-Spring的HttpInvoker学习

  Hessian和Burlap都是基于HTTP的,他们都解决了RMI所头疼的防火墙渗透问题。但当传递过来的RPC消息中包含序列化对象时,RMI就完胜Hessian和Burlap了。
  因为Hessian和Burlap都是采用了私有的序列化机制,而RMI使用的是Java本身的序列化机制。如果数据模型非常复杂,那么Hessian/Burlap的序列化模型可能就无法胜任了。
  Spring开发团队意识到RMI服务和基于HTTP的服务之前的空白,Spring的HttpInvoker应运而生。
  Spring的HttpInvoker,它基于HTTP之上提供RPC,同时又使用了Java的对象序列化机制。
  程序的具体实现
  一、首先我们创建一个实体类,并实现Serializable接口
package entity;
import java.io.Serializable;
public class Fruit implements Serializable {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
  二、创建一个接口
  package service;
  import java.util.List;
  import entity.Fruit;
  public interface FruitService {
  List<Fruit> getFruitList();
  }
  三、创建一个类,并实现步骤二中的接口
package service.impl;
import java.util.ArrayList;
import java.util.List;
import service.FruitService;
import entity.Fruit;
public class FruitServiceImpl implements FruitService {
public List<Fruit> getFruitList() {
List<Fruit> list = new ArrayList<Fruit>();
Fruit f1 = new Fruit();
f1.setName("橙子");
f1.setColor("黄色");
Fruit f2 = new Fruit();
f2.setName("苹果");
f2.setColor("红色");
list.add(f1);
list.add(f2);
return list;
}
}
 四、在WEB-INF下的web.xml中配置SpringMVC需要的信息
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
  五、在applicationContext.xml配置需要导出服务的bean信息
  <bean id="furitService" class="service.impl.FruitServiceImpl"></bean>
  <bean id="FuritService"
  class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
  p:serviceInterface="service.FruitService" p:service-ref="furitService" />
  六、在WEB-INF下创建springMvc-servlet.xml文件,并配置urlMapping
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/fruitService">FuritService</prop>
</props>
</property>
</bean>
</beans>
  七、在applicationContext.xml编写客户端所需要获得服务的bean信息
  <bean id="getFruitService"
  class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
  p:serviceInterface="service.FruitService"
  p:serviceUrl="http://localhost:8080/SpringHttpInvoker/fruitService" />
  八、编写测试代码
package test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.Fruit;
import service.FruitService;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
FruitService fruitService = (FruitService) ctx
.getBean("getFruitService");
List<Fruit> fruitList = fruitService.getFruitList();
for (Fruit fruit : fruitList) {
System.out.println(fruit.getColor() + "的" + fruit.getName());
}
}
}
  将项目部署到Tomcat上,启动Tomcat服务,并运行测试代码

posted on 2014-11-19 09:59 顺其自然EVO 阅读(217) 评论(0)  编辑  收藏 所属分类: 测试学习专栏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜