国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java學習之路-Spring的HttpInvoker學習

2019-11-14 21:45:56
字體:
來源:轉載
供稿:網友
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服務,并運行測試代碼 ===========控制臺========

黃色的橙子 紅色的蘋果

=======================


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普安县| 新泰市| 沅江市| 南汇区| 长武县| 丰都县| 杨浦区| 华池县| 龙泉市| 普兰县| 克什克腾旗| 永和县| 重庆市| 宁夏| 五台县| 临泉县| 北安市| 观塘区| 霍州市| 东兴市| 博客| 宜章县| 四平市| 临城县| 湘潭市| 芒康县| 游戏| 万全县| 阳高县| 樟树市| 封丘县| 乐山市| 读书| 永顺县| 洪湖市| 抚州市| 班玛县| 左权县| 赤壁市| 青神县| 隆安县|