HttpInvokerProxyFactoryBean为Spring特有的实现方式,同样它也是基于http的
其中,配置服务端有两种方式
第一种基于HttpInvokerServiceExporter,这个是依赖于Spring mvc来实现的
<bean id="accountService" class="example.AccountServiceImpl">
</bean>
<bean name="/AccountService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
<!-- 也可以用下面的方法,由控制器转发 -->
<bean name="accountExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
同时还要求在web.xml中配置如下servlet
<servlet>
<servlet-name>accountExporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>accountExporter</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
第二种不依赖于web容器,可以直接用main调用既可
<bean id="accountService" class="example.AccountServiceImpl">
</bean>
<bean name="accountExporter"
class="org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
<bean id="httpServer"
class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
<property name="contexts">
<map>
<entry key="/remoting/AccountService" value-ref="accountExporter"/>
</map>
</property>
<property name="port" value="8080" />
</bean>
客户端代码如下。
<bean id="accountService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/remoting/AccountService"/>
<property name="serviceInterface" value="example.AccountService"/>
<!-- 可选,默认为SimpleHttpInvokerRequestExecutor实现方式,这里可以选择为HttpComponents实现的客户端,这里还必须要加入org.apache.httpcomponents:httpclient:4.3.5的依赖 -->
<property name="httpInvokerRequestExecutor">
<bean class="org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor"/>
</property>
</bean>
上面的httpInvokerRequestExecutor可以设置默认实现
到此,对于Spring的rmi功能就结束了。
本文介绍了Spring框架中HttpInvoker的两种服务端配置方式:一种依赖于Spring MVC,另一种则不依赖于Web容器。同时,也展示了如何在客户端进行配置以实现远程调用。

739

被折叠的 条评论
为什么被折叠?



