项目之间进行通讯,会用到很多远程调用技术,今天讲一下httpinvoker的详细配置,具体的配置过程如下
1.1在服务器端项目上,web.xml配置servlet
示例:
<servlet> <servlet-name>rmiHttp</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>contextConfigLocation</param-name> <!-- 自定义位置 --> <param-value> classpath:config/rmi/rmiHttp-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>rmiHttp</servlet-name> <url-pattern>/rmi/*</url-pattern> </servlet-mapping>
示例:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean name="rmiService" class="com.web.jutong.rmi.RmiServiceImpl"></bean> <bean name="/rmiHttp" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="rmiService" ></property> <property name="serviceInterface" value="com.web.jutong.rmi.IRmiService" ></property> </bean> </beans>
第一个bean 是实现类bean 在下面实际使用的bean中使用到了。
第二个bean 是真正的服务,bean的名称要与servlet名称相同,也就是提供的服务。class需要是
org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter
或者相关的类,才能达到使用httpinvoker的目的。
service 就是引用实现类
serviceInterface指向上层接口类
1.3 后台代码实现
package com.web.jutong.rmi; public interface IRmiService { public String doQueryVideo(String termno,String start,String end); }
package com.web.jutong.rmi; import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter; import org.springframework.stereotype.Service; @Service("rmiService") public class RmiServiceImpl implements IRmiService { @Override public String doQueryVideo(String termno, String start, String end) { // TODO Auto-generated method stub return "123"; } }
2.1独立的bean配置(http-invoker.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean id="rmiHttpService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://127.0.0.1:8080/demo/rmi/rmiHttp"></property> <property name="serviceInterface" value="com.jutong.web.rmi.IRmiService"></property> </bean> </beans>
2.2spring配置修改
contextConfigLocation 增加路径设置
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 自定义位置 -->
<param-value>
classpath:config/http-invoker.xml,
classpath:config/spring-*.xml</param-value>
</init-param>
2.3客户端接口
复制服务器端的接口文件到客户端,serviceInterface指向本地的接口地址。
2.4客户端调用
@RequestMapping("/query/result") @ResponseBody public String doQueryResult(String termno,String start,String end) { RmiUtil rmi =new RmiUtil(); String result=rmi.getService(this.ac).doQueryVideo(termno, start, end); return result; }
package com.jutong.util; import org.springframework.context.ApplicationContext; import com.jutong.web.rmi.IRmiService; public class RmiUtil { private IRmiService service=null; public IRmiService getService(ApplicationContext context){ if(null==service) { service=context.getBean("rmiHttpService",IRmiService.class); } return service; } }
本例详细解说了httpinvoker的详细配置,因为字母太多,改用了rmi作为代替,实际上是用的httpinvoker技术。笔者也百度看了很多示例,但都说的不够明白,摸索了很久了,花了一天时间 , 终于搞定了。希望这篇文章能够让你更清晰的理解。