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 閱讀(226) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大渡口区| 云浮市| 广元市| 抚州市| 苏尼特左旗| 濉溪县| 九江县| 鞍山市| 巩义市| 楚雄市| 观塘区| 木里| 乐陵市| 施甸县| 精河县| 洛川县| 华宁县| 绵阳市| 伊川县| 开原市| 道孚县| 双桥区| 呼玛县| 政和县| 阳曲县| 农安县| 靖边县| 盘锦市| 北碚区| 法库县| 元阳县| 收藏| 新田县| 普洱| 松江区| 兖州市| 九龙坡区| 宜君县| 云林县| 山阴县| 清水河县|