spring_远程调用

释放双眼,带上耳机,听听看~!

参考文章:

Spring in Action 学习笔记—第六章 远程调用

spring的httpInvoker远程调用 示例

《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用

Spring HTTP invoker简介

 

例子:远程服务器上提供了计算两个数的和的接口,利用spring的httpinvoker技术在客户端实现远程调用,将计算功能交给服务器来执行,然后服务器将结果返回给客户端。

 

服务器端结构:

spring_远程调用

 

客户端机构:

spring_远程调用

其中GetSumService.jar是服务器端提供给客户端使用的接口,用来计算两个数的和。就是服务器端com.yuan.service打出的jar包。

 

服务器端代码分析:

计算接口(TestService.java):


1
2
3
4
5
6
7
8
9
10
11 package com.yuan.service;
22
33 /**
44  * @author Caihanyuan
55  * @time 2012-10-26 上午10:56:46
66  */
77 public interface TestService {
88     public double getSum(double numberA, double numberB);
99 }
10

计算接口的实现(TestServiceImp.java):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
11 package com.sunflower.serviceimp;
2 2
3 3 import com.yuan.service.TestService;
4 4
5 5 /**
6 6  * @author Caihanyuan
7 7  * @time 2012-10-26 上午10:57:23
8 8  */
9 9 public class TestServiceImp implements TestService {
1010
1111     @Override
1212     public double getSum(double numberA, double numberB) {
1313         return numberA + numberB;
1414     }
1515
1616 }
17

在spring配置文件中声明服务的出口(application-context.xml):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
11 <?xml version="1.0" encoding="UTF-8"?>
2 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
3 3 "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
4 4 <beans>
5 5     <bean name="httpService"
6 6         class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
7 7         <property name="service">
8 8             <ref bean="hDao" />
9 9         </property>
1010         <property name="serviceInterface">
1111             <value>com.yuan.service.TestService</value>
1212         </property>
1313     </bean>
1414    
1515     <bean id="hDao" class="com.sunflower.serviceimp.TestServiceImp" />
1616    
1717 </beans>
18

在org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter配置服务的接口和接口的实现。

配置服务器的属性(web.xml):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
11 <?xml version="1.0" encoding="UTF-8"?>
2 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5 5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6 6
7 7     <context-param>
8 8         <param-name>contextConfigLocation</param-name>
9 9         <param-value>/WEB-INF/application-context.xml</param-value>
1010     </context-param>
1111
1212     <listener>
1313         <listener-class>
1414         org.springframework.web.context.ContextLoaderListener
1515         </listener-class>
1616     </listener>
1717
1818
1919     <servlet>
2020         <servlet-name>service</servlet-name>
2121         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
2222         <load-on-startup>1</load-on-startup>
2323     </servlet>
2424
2525
2626     <servlet-mapping>
2727         <servlet-name>service</servlet-name>
2828         <url-pattern>/service/*</url-pattern>
2929     </servlet-mapping>
3030
3131
3232
3333     <welcome-file-list>
3434         <welcome-file>index.jsp</welcome-file>
3535     </welcome-file-list>
3636 </web-app>
37

第710行配置上下文路径,就是服务出口配置文件的路径。1216行配置转载上下文的监听器。19~29行配置Http请求中服务对应的路径。

配置路径映射(service-servlet.xml):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
11 <?xml version="1.0" encoding="UTF-8"?>
2 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
3 3 "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
4 4 <beans>
5 5     <bean id="urlMapping"
6 6         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
7 7         <property name="mappings">
8 8             <props>
9 9                 <prop key="/httpService">httpService</prop>
1010             </props>
1111         </property>
1212     </bean>
1313 </beans>
14

这个配置文件的名字是有规范的,后缀为
-servlet.xml,前缀是上面web.xml中配置的servlet名字。

 

 

客户端代码分析:

配置远程调用代理(application-context.xml):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
11 <?xml version="1.0" encoding="UTF-8"?>
2 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
3 3 "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
4 4 <beans>
5 5     <bean id="httpService"
6 6         class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
7 7         <property name="serviceUrl">
8 8             <value>http://localhost:80/service/service/httpService</value>
9 9         </property>
1010         <property name="serviceInterface">
1111             <value>com.yuan.service.TestService</value>
1212         </property>
1313     </bean>
1414 </beans>
15

 

测试(Test.java):****


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
11 package com.yuan.test;
2 2
3 3 import org.springframework.context.ApplicationContext;
4 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5 5
6 6 import com.yuan.service.TestService;
7 7
8 8 /**
9 9  * @author Caihanyuan
1010  * @time 2012-10-26 上午11:14:09
1111  */
1212 public class Test {
1313     private static ApplicationContext appContext = new ClassPathXmlApplicationContext(
1414             "application-context.xml");
1515
1616     public static TestService getTestService() {
1717         return (TestService) appContext.getBean("httpService");
1818     }
1919
2020     public static void main(String[] args) {
2121         double numberA = 12.5;
2222         double numberB = 52.3;
2323
2424         System.out.println("sum of numberA and numberB is:"
2525                 + getTestService().getSum(numberA, numberB));
2626
2727     }
2828 }
29

 

测试结果:

spring_远程调用

 

源码下载:
http://pan.baidu.com/share/link?shareid=87148&uk=2198762756

给TA打赏
共{{data.count}}人
人已打赏
安全经验

Google Adsense的技巧、诀窍和秘密

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索