Spring遠(yuǎn)程處理(通過Hessian示例)

借助于 HessianServiceExporter HessianProxyFactoryBean 類,我們可以實(shí)現(xiàn)hessian提供的遠(yuǎn)程服務(wù)。

Hessian的優(yōu)勢(shì)

Hessian在整個(gè)防火墻上都能很好地工作。 Hessian具有可移植性,可以與PHP和.Net等其他語言集成。

Hessian遠(yuǎn)程處理的示例

您需要?jiǎng)?chuàng)建以下文件來創(chuàng)建簡(jiǎn)單的hessian應(yīng)用程序:

Calculation.java CalculationImpl.java web.xml hessian-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>hessian</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>hessian</servlet-name>
    <url-pattern>*.http</url-pattern>
</servlet-mapping>
</web-app>

4、hessian-servlet.xml

必須在WEB-INF文件夾中創(chuàng)建。它的名稱必須是servletname-servlet.xml。它為 CalculationImpl HessianServiceExporter 定義了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.HessianServiceExporter">
    <property name="service" ref="calculationBean"></property>
    <property name="serviceInterface" value="com.nhooo.Calculation"></property>
</bean>
</beans>

5、client-beans.xml

在此xml文件中,我們?yōu)?strong> HessianProxyFactoryBean 定義了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.HessianProxyFactoryBean">
    <property name="serviceUrl" 
         value="http://localhost:8888/hessian/Calculation.http"></property>
    <property name="serviceInterface" value="com.nhooo.Calculation"></property>
</bean>
</beans>

在此示例中,我們的項(xiàng)目名稱為hessian,即用作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(5));
 }
}

如何運(yùn)行此示例

啟動(dòng)并部署項(xiàng)目,這里我們假設(shè)服務(wù)器在8888端口號(hào)上運(yùn)行。如果端口號(hào)不同,請(qǐng)更改client-beans.xml中的serviceURL。

然后,編譯并運(yùn)行Client.java文件。

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清