Houssian和Burlap均由Coucho提供。
借助于 BurlapServiceExporter 和 BurlapProxyFactoryBean 类,我们可以实现burlap提供的远程服务。 Burlap的示例与Burlap相同,您只需将Burlap更改为Burlap。
通过Burlap进行远程处理的示例
您需要创建以下文件来创建简单的Burlap应用程序:
Calculation.java
CalculationImpl.java
web.xml
burlap-servlet.xml
client-beans.xml
Client.java
Calculation.java
这是包含一个方法多维数据集的简单接口。
package com.nhooo; public interface Calculation { int cube(int number); }
CalculationImpl.java
此类提供了Calculation接口的实现。
package com.nhooo; public class CalculationImpl implements Calculation{ public int cube(int number) { return number*number*number; } }
web.xml
在此xml文件中,我们将DispatcherServlet定义为前端控制器。如果任何请求后跟.http扩展名,它将被转发到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>
burlap-servlet.xml
它必须在WEB-INF文件夹中创建。它的名称必须是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>
client-beans.xml
在此xml文件中,我们为BurlapProxyFactoryBean 定义了bean。您需要定义此类的两个属性。
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>
在此示例中,我们的项目名称为麻布,即用作serviceURL中的上下文根。
Client.java
该类获取Calculation的实例并调用多维数据集方法。
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)); } }
如何运行此示例
启动并部署项目,这里我们假设服务器在8888端口号上运行。如果端口号不同,请更改client-beans.xml中的serviceURL。
然后,编译并运行Client.java文件。
标签:xml,Burlap,示例,Calculation,Spring,number,java From: https://www.cnblogs.com/cnetsa/p/17060833.html