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