今天我們來學一下Burlap。
Burlap是一種基于xml遠程調用技術,但與其他基于XML的遠程技術(例如SOAP或者XML-RPC)不同,Burlap的消息結構盡可能的簡單,不需要額外的外部定義語言(例如WSDL或IDL)。
Burlap和Hessian很大程度上,它們是一樣的,唯一的區別在于Hessian的消息是二進制的,而Burlap的消息是XML。(Burlap和Hessian代碼實現上也很相似)
接下來我們看一下代碼的實現:
一、首先我們先創建一個實體類,這里不需要實現Serializable接口
package entity;public class Food { PRivate String name; private double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; }}二、我們來定義一個接口
package service;import java.util.List;import entity.Food;public interface FoodService { List<Food> getFoodList();}三、定義一個類,實現步驟二中的接口,并繼承BurlapServlet類(這里需要用到Burlap的jar文件,可以到這里下載http://www.findjar.com/jar/burlap/jars/burlap-2.1.7.jar.html)
package service.impl;import java.util.ArrayList;import java.util.List;import service.FoodService;import com.caucho.burlap.server.BurlapServlet;import entity.Food;public class FoodServiceImpl extends BurlapServlet implements FoodService { public List<Food> getFoodList() { List<Food> list=new ArrayList<Food>(); Food f1=new Food(); f1.setName("酸菜魚"); f1.setPrice(25); Food f2=new Food(); f2.setName("糖醋魚"); f2.setPrice(23); list.add(f1); list.add(f2); return list; }}四、現在我們可以在WEB-INF下的web.xml中配置一個servlet(Hessian也可以這樣配置servlet)
<servlet> <servlet-name>food</servlet-name> <servlet-class>service.impl.FoodServiceImpl</servlet-class></servlet><servlet-mapping> <servlet-name>food</servlet-name> <url-pattern>/food</url-pattern></servlet-mapping>
五、我們來寫一下測試代碼,看一下結果
package test;import java.util.List;import service.FoodService;import com.caucho.burlap.client.BurlapProxyFactory;import entity.Food;public class Test { public static void main(String[] args) { String url="http://127.0.0.1:8080/test/food"; BurlapProxyFactory factory=new BurlapProxyFactory(); try { FoodService foodSevice=(FoodService) factory.create(FoodService.class, url); List<Food> foodList = foodSevice.getFoodList(); for (Food food : foodList) { System.out.println(food.getName()+":"+food.getPrice()+"元。"); } } catch (Exception e) { e.printStackTrace(); } }}控制臺顯示的結果為:
=========控制臺=========
酸菜魚:25.0元。
糖醋魚:23.0元。
========================
接下來我們看一下Spring整合Burlap,這里和Spring整合Hessian基本差不多。
Spring整合Burlap一、我們來定義一個接口
package service;import java.util.List;import entity.Food;public interface FoodService { List<Food> getFoodList();}二、定義一個類,實現步驟二中的接口
package service.impl;import java.util.ArrayList;import java.util.List;import service.FoodService;import entity.Food;public class FoodServiceImpl implements FoodService { public List<Food> getFoodList() { List<Food> list=new ArrayList<Food>(); Food f1=new Food(); f1.setName("酸菜魚"); f1.setPrice(25); Food f2=new Food(); f2.setName("糖醋魚"); f2.setPrice(23); 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="foodService" class="service.impl.FoodServiceImpl"></bean><bean id="FoodService" class="org.springframework.remoting.caucho.BurlapServiceExporter" p:serviceInterface="service.FoodService" p:service-ref="foodService"/>
五、在WEB-INF下新建springMvc-servlet.xml文件,并配置信息
<?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="/foodService">FoodService</prop> </props> </property> </bean></beans>
六、在客戶端程序applicationContext.xml中配置獲取服務的bean信息
<bean id="getFoodService" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean" p:serviceInterface="service.FoodService" p:serviceUrl="http://127.0.0.1:8080/test/foodService"/>
七、現在我們編寫測代碼
package test;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import entity.Food;import service.FoodService;public class Test { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); FoodService foodService=(FoodService) ctx.getBean("getFoodService"); List<Food> foodList = foodService.getFoodList(); for (Food food : foodList) { System.out.println(food.getName()+":"+food.getPrice()+"元。"); } }}接下來我們把項目部署到Tomcat上面,并且啟動服務。運行測試代碼
======控制臺=======
酸菜魚:25.0元。
糖醋魚:23.0元。
===================
到這里我們已經學習了Spring整合Burlap。
新聞熱點
疑難解答