亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

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

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

Hessian的優(yōu)勢

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

Hessian遠程處理的示例

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

Calculation.java CalculationImpl.java web.xml hessian-servlet.xml client-beans.xml Client.java

1、Calculation.java

這是包含一個方法多維數(shù)據(jù)集的簡單接口。

package com.nhooo;
public interface Calculation {
int cube(int number);
}

2、CalculationImpl.java

此類提供了Calculation接口的實現(xiàn)。

package com.nhooo;
public class CalculationImpl implements Calculation{
    public int cube(int number) {
        return number*number*number;
    }
}

3、web.xml

在此xml文件中,我們將DispatcherServlet定義為前端控制器。如果任何請求后跟.http擴展名,它將被轉(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。您需要定義此類的兩個屬性。

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>

在此示例中,我們的項目名稱為hessian,即用作serviceURL中的上下文根。


6、Client.java

該類獲取Calculation的實例并調(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));
 }
}

如何運行此示例

啟動并部署項目,這里我們假設服務器在8888端口號上運行。如果端口號不同,請更改client-beans.xml中的serviceURL。

然后,編譯并運行Client.java文件。