Spring 客户端连接服务
Spring 提供了 HttpInvokerProxyFactoryBean 工厂 bean 连接服务。类似于 Hessian 的 HessianProxyFactoryBean ,配置 HttpInvokerProxyFactoryBean 时,只需指定服务的
url 以及服务实现的接口。通过使用代理, Spring 可将调用转换
Spring提供了HttpInvokerProxyFactoryBean工厂bean连接服务。类似于Hessian的HessianProxyFactoryBean,配置HttpInvokerProxyFactoryBean时,只需指定服务的url以及服务
实现的接口。通过使用代理,Spring可将调用转换成POST请求发送到指定服务。详细的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的文件头,包含dtd等信息-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!-- Spring配置文件的根元素-->
<beans>
<!-- 配置测试bean,该测试bean依赖于远程服务bean-->
<bean id="test" class="lee.Test">
<!-- 配置依赖注入-->
<property name="hello">
<ref local="helloService"/>
</property>
</bean>
<!-- 配置连接远程服务的bean-->
<bean id="helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<!-- 远程服务的url-->
<property name="serviceUrl" value="http://localhost:8888/httpInvoker/remoting/helloService"/>
<!-- 远程服务所实现的接口-->
<property name="serviceInterface" value="lee.Hello"/>
</bean>
</beans>
通过上面的配置,客户端可以访问httpInvoker提供的服务,默认情况下,HttpInvokerPropxy使用J2SE的HTTP Client功能。可通过设置httpInvokerRequestExecutor属性使用
Commons HttpClient。通过对配置文件简单修改,将
<bean id="helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<!-- 远程服务的url-->
<property name="serviceUrl" value="http://localhost:8888/httpInvoker/remoting/helloService"/>
<!-- 远程服务所实现的接口-->
<property name="serviceInterface" value="lee.Hello"/>
</bean>
修改成:
<bean id="helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<!-- 远程服务的url-->
<property name="serviceUrl" value="http://localhost:8888/httpInvoker/remoting/helloService"/>
<!-- 远程服务所实现的接口-->
<property name="serviceInterface" value="lee.Hello"/>
<!-- 指定使用Commons HttpClient功能-->
<property name="httpInvokerRequestExecutor">
<bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"/>
</property>
</bean>
Spring 提供了 HttpInvokerProxyFactoryBean 工厂 bean 连接服务。类似于 Hessian 的 HessianProxyFactoryBean ,配置 HttpInvokerProxyFactoryBean 时,只需指定服务的
url 以及服务实现的接口。通过使用代理, Spring 可将调用转换
Spring提供了HttpInvokerProxyFactoryBean工厂bean连接服务。类似于Hessian的HessianProxyFactoryBean,配置HttpInvokerProxyFactoryBean时,只需指定服务的url以及服务
实现的接口。通过使用代理,Spring可将调用转换成POST请求发送到指定服务。详细的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的文件头,包含dtd等信息-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!-- Spring配置文件的根元素-->
<beans>
<!-- 配置测试bean,该测试bean依赖于远程服务bean-->
<bean id="test" class="lee.Test">
<!-- 配置依赖注入-->
<property name="hello">
<ref local="helloService"/>
</property>
</bean>
<!-- 配置连接远程服务的bean-->
<bean id="helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<!-- 远程服务的url-->
<property name="serviceUrl" value="http://localhost:8888/httpInvoker/remoting/helloService"/>
<!-- 远程服务所实现的接口-->
<property name="serviceInterface" value="lee.Hello"/>
</bean>
</beans>
通过上面的配置,客户端可以访问httpInvoker提供的服务,默认情况下,HttpInvokerPropxy使用J2SE的HTTP Client功能。可通过设置httpInvokerRequestExecutor属性使用
Commons HttpClient。通过对配置文件简单修改,将
<bean id="helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<!-- 远程服务的url-->
<property name="serviceUrl" value="http://localhost:8888/httpInvoker/remoting/helloService"/>
<!-- 远程服务所实现的接口-->
<property name="serviceInterface" value="lee.Hello"/>
</bean>
修改成:
<bean id="helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<!-- 远程服务的url-->
<property name="serviceUrl" value="http://localhost:8888/httpInvoker/remoting/helloService"/>
<!-- 远程服务所实现的接口-->
<property name="serviceInterface" value="lee.Hello"/>
<!-- 指定使用Commons HttpClient功能-->
<property name="httpInvokerRequestExecutor">
<bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"/>
</property>
</bean>
本文介绍如何使用Spring框架的HttpInvokerProxyFactoryBean进行远程服务调用。通过简单的配置即可实现基于HTTP协议的服务交互,支持使用J2SE的HTTPClient或CommonsHttpClient。

2万+

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



