您的位置:首页 > 理论基础 > 计算机网络

Spring HTTP invoker简介

2013-06-15 15:59 459 查看
UcService service = (UcService) WebApplicationContextUtils

.getRequiredWebApplicationContext(

request.getSession().getServletContext()).getBean(

"httpService");

UserInfo user = service.getUserInfobyName("hanqunfeng");


<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/httpService">httpService</prop> </props> </property> </bean>


Spring HTTP invoker简介 Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,这有点类似于we

Spring HTTP invoker简介

Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,这有点类似于webservice,但又不同于webservice,区别如下:

webservice

HTTP invoker

跨平台,跨语言

只支持java语言

支持SOAP,提供wsdl

不支持

结构庞大,依赖特定的webservice实现,如xfire等

结构简单,只依赖于spring框架本身

项目中使用哪种远程调用机制取决于项目本身的要求。

Server端:

1、在context中注册bean

<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">

<props>
<prop key="/httpService">httpService</prop>
</props>
</property>
</bean>


2、 在web.xml中注册servlet

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml
</param-value>

</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>service</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>

<servlet-name>service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>


3、 在WEB-INF下增加service-servlet.xml(文件名为 web.xml中的servlet名称(此处为service)+"-servlet.xml")

<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">

<props>
<prop key="/httpService">httpService</prop>
</props>
</property>
</bean>


于是发布成http://${serviceName}:${port}/${contextPath}/service/httpService 的服务。

客户端

1、 配置bean

<bean id="httpService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value> http://${serviceName}:${port}/${contextPath}/service/httpService </value>
</property>
<property name="serviceInterface"
value="com.netqin.baike.service.UcService">
</property>
</bean>


2、 按照普通bean的获取方式获得实例,然后调用接口:
UcService service = (UcService) WebApplicationContextUtils.getRequiredWebApplicationContext(
request.getSession().getServletContext()).getBean("httpService");

UserInfo user = service.getUserInfobyName("hanqunfeng");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: