English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring 리모트 처리(通过Hessian示例)

借助 HessianServiceExporter HessianProxyFactoryBean 클래스를 통해 Hessian이 제공하는 리모트 서비스를 구현할 수 있습니다.

Hessian의 장점

Hessian은 전체 방화벽에서 잘 작동합니다. Hessian은 이동성이 있으며 PHP와 .Net 등 다른 언어와 통합할 수 있습니다.

Hessian 리모트 처리 예제

간단한 hessian 애플리케이션을 생성하기 위해 다음 파일을 생성해야 합니다:

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

1、Calculation.java

이 인터페이스는 하나의 메서드를 포함한 간단한 인터페이스입니다.

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

2、CalculationImpl.java

이 클래스는 Calculation 인터페이스의 구현을 제공합니다.

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

3、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>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 폴더에 생성됩니다. 이름은 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.w3codebox.CalculationImpl"></bean>
<bean name="/Calculation.http" 
class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="calculationBean"></property>
    <property name="serviceInterface" value="com.w3codebox.Calculation"></property>
</bean>
</beans>

5client-beans.xml

이 xml 파일에서는 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.w3codebox.Calculation"></property>
</bean>
</beans>

이 예제에서는 프로젝트 이름이 hessian로, serviceURL의 콘텍스트 루트로 사용됩니다.


6Client.java

이 클래스는 Calculation의 인스턴스를 얻고 다차원 데이터 집합 메서드를 호출합니다.

package com.w3codebox;
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 파일을 컴파일하고 실행합니다.