参考文章:
Spring in Action 学习笔记—第六章 远程调用
《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用
例子:远程服务器上提供了计算两个数的和的接口,利用spring的httpinvoker技术在客户端实现远程调用,将计算功能交给服务器来执行,然后服务器将结果返回给客户端。
服务器端结构:
客户端机构:
其中GetSumService.jar是服务器端提供给客户端使用的接口,用来计算两个数的和。就是服务器端com.yuan.service打出的jar包。
服务器端代码分析:
计算接口(TestService.java):
1 package com.yuan.service; 2 3 /** 4 * @author Caihanyuan 5 * @time 2012-10-26 上午10:56:46 6 */ 7 public interface TestService { 8 public double getSum(double numberA, double numberB); 9 }
计算接口的实现(TestServiceImp.java):
1 package com.sunflower.serviceimp; 2 3 import com.yuan.service.TestService; 4 5 /** 6 * @author Caihanyuan 7 * @time 2012-10-26 上午10:57:23 8 */ 9 public class TestServiceImp implements TestService { 10 11 @Override 12 public double getSum(double numberA, double numberB) { 13 return numberA + numberB; 14 } 15 16 }
在spring配置文件中声明服务的出口(application-context.xml):
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 3 "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 4 <beans> 5 <bean name="httpService" 6 class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> 7 <property name="service"> 8 <ref bean="hDao" /> 9 </property> 10 <property name="serviceInterface"> 11 <value>com.yuan.service.TestService</value> 12 </property> 13 </bean> 14 15 <bean id="hDao" class="com.sunflower.serviceimp.TestServiceImp" /> 16 17 </beans>
在org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter配置服务的接口和接口的实现。
配置服务器的属性(web.xml):
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>/WEB-INF/application-context.xml</param-value> 10 </context-param> 11 12 <listener> 13 <listener-class> 14 org.springframework.web.context.ContextLoaderListener 15 </listener-class> 16 </listener> 17 18 19 <servlet> 20 <servlet-name>service</servlet-name> 21 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 22 <load-on-startup>1</load-on-startup> 23 </servlet> 24 25 26 <servlet-mapping> 27 <servlet-name>service</servlet-name> 28 <url-pattern>/service/*</url-pattern> 29 </servlet-mapping> 30 31 32 33 <welcome-file-list> 34 <welcome-file>index.jsp</welcome-file> 35 </welcome-file-list> 36 </web-app>
第7~10行配置上下文路径,就是服务出口配置文件的路径。12~16行配置转载上下文的监听器。19~29行配置Http请求中服务对应的路径。
配置路径映射(service-servlet.xml):
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 3 "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 4 <beans> 5 <bean id="urlMapping" 6 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 7 <property name="mappings"> 8 <props> 9 <prop key="/httpService">httpService</prop> 10 </props> 11 </property> 12 </bean> 13 </beans>
这个配置文件的名字是有规范的,后缀为-servlet.xml,前缀是上面web.xml中配置的servlet名字。
客户端代码分析:
配置远程调用代理(application-context.xml):
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 3 "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 4 <beans> 5 <bean id="httpService" 6 class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> 7 <property name="serviceUrl"> 8 <value>http://localhost:80/service/service/httpService</value> 9 </property> 10 <property name="serviceInterface"> 11 <value>com.yuan.service.TestService</value> 12 </property> 13 </bean> 14 </beans>
测试(Test.java):
1 package com.yuan.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.yuan.service.TestService; 7 8 /** 9 * @author Caihanyuan 10 * @time 2012-10-26 上午11:14:09 11 */ 12 public class Test { 13 private static ApplicationContext appContext = new ClassPathXmlApplicationContext( 14 "application-context.xml"); 15 16 public static TestService getTestService() { 17 return (TestService) appContext.getBean("httpService"); 18 } 19 20 public static void main(String[] args) { 21 double numberA = 12.5; 22 double numberB = 52.3; 23 24 System.out.println("sum of numberA and numberB is:" 25 + getTestService().getSum(numberA, numberB)); 26 27 } 28 }
测试结果:
源码下载:http://pan.baidu.com/share/link?shareid=87148&uk=2198762756