RMI在Spring中的使用之HttpInvokerServiceExporter

本文介绍了Spring框架中HttpInvoker的两种服务端配置方式:一种依赖于Spring MVC,另一种则不依赖于Web容器。同时,也展示了如何在客户端进行配置以实现远程调用。

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功能就结束了。

转载于:https://my.oschina.net/stategrace/blog/338290

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值