?
This document uses PHP Chinese website manual Release
與使用自身序列化機(jī)制的輕量級(jí)協(xié)議Burlap和Hessian相反,Spring HTTP調(diào)用器使用標(biāo)準(zhǔn)Java序列化機(jī)制來通過HTTP暴露業(yè)務(wù)。如果你的參數(shù)或返回值是復(fù)雜類型,并且不能通過Hessian和Burlap的序列化機(jī)制進(jìn)行序列化,HTTP調(diào)用器就很有優(yōu)勢(shì)(參閱下一節(jié),選擇遠(yuǎn)程技術(shù)時(shí)的考慮)。
實(shí)際上,Spring可以使用J2SE提供的標(biāo)準(zhǔn)功能或Commons的HttpClient來實(shí)現(xiàn)HTTP調(diào)用。如果你需要更先進(jìn),更容易使用的功能,就使用后者。你可以參考 jakarta.apache.org/commons/httpclient。
為服務(wù)對(duì)象設(shè)置HTTP調(diào)用器和你在Hessian或Burlap中使用的方式類似。就象為Hessian支持提供的 HessianServiceExporter
,Spring的HTTP調(diào)用器提供了 org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter
。
為了在Spring Web MVC DispatcherServlet
中暴露AccountService
(如上所述), 需要在dispatcher的應(yīng)用上下文中使用以下配置:
<bean name="/AccountService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
和在Hessian章節(jié)講的一樣,這個(gè)exporter定義將通過 DispatcherServlet
標(biāo)準(zhǔn)的映射工具暴露出來。
做為可選項(xiàng), 在你的根應(yīng)用上下文中(比如'WEB-INF/applicationContext.xml'
)創(chuàng)建一個(gè)HttpInvokerServiceExporter
:
<bean name="accountExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
另外,在'web.xml'
中為這個(gè)exporter定義一個(gè)相應(yīng)的servlet,其名稱與目標(biāo)exporter bean的名稱相匹配:
<servlet> <servlet-name>accountExporter</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>accountExporter</servlet-name> <url-pattern>/remoting/AccountService</url-pattern> </servlet-mapping>
同樣,從客戶端連接業(yè)務(wù)與你使用Hessian或Burlap時(shí)所做的很相似。使用代理,Spring可以將你調(diào)用的HTTP POST請(qǐng)求轉(zhuǎn)換成被暴露服務(wù)的URL。
<bean id="httpInvokerProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
就象上面說的一樣,你可以選擇使用你想使用的HTTP客戶端。缺省情況下,HttpInvokerProxy
使用J2SE的HTTP功能,但是你也可以通過設(shè)置httpInvokerRequestExecutor
屬性選擇使用Commons HttpClient
:
<property name="httpInvokerRequestExecutor"> <bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"/> </property>