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服務,並運行測試代碼
===========控制台========
黃色的橙子
紅色的蘋果
=======================