Houssian和Burlap均由Coucho提供。
借助于 BurlapServiceExporter 和 BurlapProxyFactoryBean 類,我們可以實(shí)現(xiàn)burlap提供的遠(yuǎn)程服務(wù)。 Burlap的示例與Burlap相同,您只需將Burlap更改為Burlap。
您需要?jiǎng)?chuàng)建以下文件來(lái)創(chuàng)建簡(jiǎn)單的Burlap應(yīng)用程序:
Calculation.java CalculationImpl.java web.xml burlap-servlet.xml client-beans.xml Client.java
1、Calculation.java
這是包含一個(gè)方法多維數(shù)據(jù)集的簡(jiǎn)單接口。
package com.nhooo; public interface Calculation { int cube(int number); }
2、CalculationImpl.java
此類提供了Calculation接口的實(shí)現(xiàn)。
package com.nhooo; public class CalculationImpl implements Calculation{ public int cube(int number) { return number*number*number; } }
3、web.xml
在此xml文件中,我們將DispatcherServlet定義為前端控制器。如果任何請(qǐng)求后跟.http擴(kuò)展名,它將被轉(zhuǎn)發(fā)到DispatcherServlet。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>burlap</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>burlap</servlet-name> <url-pattern>*.http</url-pattern> </servlet-mapping> </web-app>
4、burlap-servlet.xml
它必須在WEB-INF文件夾中創(chuàng)建。它的名稱必須是servletname-servlet.xml。它為 CalculationImpl 和 BurlapServiceExporter 定義了bean。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="calculationBean" class="com.nhooo.CalculationImpl"></bean> <bean name="/Calculation.http" class="org.springframework.remoting.caucho.BurlapServiceExporter"> <property name="service" ref="calculationBean"></property> <property name="serviceInterface" value="com.nhooo.Calculation"></property> </bean> </beans>
5、client-beans.xml
在此xml文件中,我們?yōu)? BurlapProxyFactoryBean 定義了bean。您需要定義此類的兩個(gè)屬性。
serviceUrl serviceInterface
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="calculationBean" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8888/burlap/Calculation.http"></property> <property name="serviceInterface" value="com.nhooo.Calculation"></property> </bean> </beans>
在此示例中,我們的項(xiàng)目名稱為麻布,即用作serviceURL中的上下文根。
6、Client.java
該類獲取Calculation的實(shí)例并調(diào)用多維數(shù)據(jù)集方法。
package com.nhooo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml"); Calculation calculation = (Calculation)context.getBean("calculationBean"); System.out.println(calculation.cube(3)); } }
啟動(dòng)并部署項(xiàng)目,這里我們假設(shè)服務(wù)器在8888端口號(hào)上運(yùn)行。如果端口號(hào)不同,請(qǐng)更改client-beans.xml中的serviceURL。
然后,編譯并運(yùn)行Client.java文件。