RMI在Spring中的使用之HttpInvokerServiceExporter

HttpInvokerProxyFactoryBean爲Spring特有的實現方式,一樣它也是基於http的java

其中,配置服務端有兩種方式web

第一種基於HttpInvokerServiceExporter,這個是依賴於Spring mvc來實現的
spring

<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中配置以下servletapache

<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調用既可mvc

<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>


客戶端代碼以下。app

<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能夠設置默認實現url

到此,對於Spring的rmi功能就結束了。
code

相關文章
相關標籤/搜索