java spring invoker环境_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 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 getFruitList() {

List list = new ArrayList();

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需要的信息

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

springMvc

org.springframework.web.servlet.DispatcherServlet

1

springMvc

/

五、在applicationContext.xml配置需要导出服务的bean信息

class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"

p:serviceInterface="service.FruitService" p:service-ref="furitService" />

六、在WEB-INF下创建springMvc-servlet.xml文件,并配置urlMapping

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">

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

FuritService

七、在applicationContext.xml编写客户端所需要获得服务的bean信息

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 fruitList = fruitService.getFruitList();

for (Fruit fruit : fruitList) {

System.out.println(fruit.getColor() + "的" + fruit.getName());

}

}

}

将项目部署到Tomcat上,启动Tomcat服务,并运行测试代码

===========控制台========

黄色的橙子

红色的苹果

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值