基于Spring MVC, 使用Http Service Invoke远程调用方法

(参考: http://blog.csdn.net/hanqunfeng/article/details/4303127)

步骤:

1. 本地定义接口,并在配置文件中说明

PersonService.java

  1. package com.anialy.httpservice.service;
  2. import com.anialy.httpservice.entity.Person;
  3. public interface PersonService {
  4. public Person getPersonByName(String name);
  5. }

PersonService.java

  1. package com.anialy.httpservice.service.impl;
  2. import com.anialy.httpservice.entity.Person;
  3. import com.anialy.httpservice.service.PersonService;
  4. public class PersonServiceImpl implements PersonService{
  5. public Person getPersonByName(String name) {
  6. if("anialy".equals(name))
  7. return new Person("anialy", 100);
  8. return null;
  9. }
  10. }

applicationContext-server-http-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 指定Spring配置文件的Schema信息 -->
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  12. <bean id="httpService"
  13. class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
  14. <property name="service">
  15. <ref bean="personService" />
  16. </property>
  17. <property name="serviceInterface" value="com.anialy.httpservice.service.PersonService">
  18. </property>
  19. </bean>
  20. <bean id="personService" class="com.anialy.httpservice.service.impl.PersonServiceImpl"/>
  21. </beans>

2. mvc配置服务uri与对应的service

applicationContext-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  8. <mvc:view-controller path="/" view-name="redirect:/home" />
  9. <mvc:view-controller path="/home" view-name="home" />
  10. <!-- Spring Service Invoke -->
  11. <bean
  12. class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  13. <property name="mappings">
  14. <props>
  15. <prop key="/service/httpService">httpService</prop>
  16. </props>
  17. </property>
  18. </bean>
  19. <!-- Spring MVC -->
  20. <bean
  21. class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  22. <property name="mappings">
  23. <value>
  24. /test=testController
  25. </value>
  26. </property>
  27. <property name="order" value="1" />
  28. </bean>
  29. <bean id="testController" class="com.anialy.webproj.controller.TestController">
  30. <property name="methodNameResolver" ref="paramResolver" />
  31. </bean>
  32. <!-- 定义JSP -->
  33. <bean
  34. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  35. <property name="prefix" value="/WEB-INF/views/" />
  36. <property name="suffix" value=".jsp" />
  37. </bean>
  38. <bean id="paramResolver"
  39. class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
  40. <property name="paramName" value="action" />
  41. <property name="defaultMethodName" value="test" />
  42. </bean>
  43. </beans>

3.  客户端配置uri的invoke

applicationContext-client-http-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 指定Spring配置文件的Schema信息 -->
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  12. <bean id="personService"
  13. class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
  14. depends-on="propertyConfigurer">
  15. <property name="serviceUrl" >
  16. <value>
  17. http://${host}:${port}/${contextPath}/service/httpService
  18. </value>
  19. </property>
  20. <property name="serviceInterface" value="com.anialy.httpservice.service.PersonService">
  21. </property>
  22. </bean>
  23. </beans>

设置属性文件

system.properties

  1. serviceName=localhost
  2. host=127.0.0.1
  3. port=8080
  4. contextPath=WebProj

加载配置文件的xml

applicationContext-constants.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  7. <bean id="propertyConfigurer"
  8. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  9. <property name="locations">
  10. <list>
  11. <value>classpath:jdbc.properties</value>
  12. <value>classpath:system.properties</value>
  13. </list>
  14. </property>
  15. </bean>
  16. </beans>

4. 编写测试方法

TestHttpServiceInvoke.java

  1. package httpservice;
  2. import java.io.IOException;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. import com.anialy.httpservice.entity.Person;
  10. import com.anialy.httpservice.service.PersonService;
  11. public class TestHttpServiceInvoke {
  12. private static final Logger logger
  13. = LoggerFactory.getLogger(TestHttpServiceInvoke.class);
  14. private ApplicationContext ctx;
  15. private PersonService personService;
  16. @Before
  17. public void init() throws IOException{
  18. ctx = new ClassPathXmlApplicationContext(new String[]{
  19. "classpath*:/applicationContext-client-http-service.xml",
  20. "classpath*:/applicationContext-constants.xml"
  21. });
  22. personService = (PersonService)  ctx.getBean("personService");
  23. System.out.println("");
  24. }
  25. @Test
  26. public void test() {
  27. Person person = personService.getPersonByName("anialy");
  28. logger.info("姓名:" + person.getName());
  29. logger.info("年龄:" + person.getAge());
  30. }
  31. }


输出:


Spring HTTP Service的更多相关文章

  1. 整合Spring时Service层为什么不做全局包扫描详解

    合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两个 ...

  2. 测试必须学spring RESTful Service(上)

    文末我会说说为什么测试必须学spring. REST REST,是指REpresentational State Transfer,有个精辟的解释什么是RESTful, 看url就知道要什么 看met ...

  3. spring盒springMVC整合父子容器问题:整合Spring时Service层为什么不做全局包扫描详解

    整合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两 ...

  4. Spring的Service层与Dao层解析

    本文转载于网络,觉得写得很透彻. dao完成连接数据库修改删除添加等的实现细节,例如sql语句是怎么写的,怎么把对象放入数据库的.service层是面向功能的,一个个功能模块比如说银行登记并完成一次存 ...

  5. spring -mvc service层调用工具类配置

    在service层时调用工具类时服务返回工具类对象为空 在此工具类上加上@Component注解就可以了 @Component:把普通pojo实例化到spring容器中,相当于配置文件中的 <b ...

  6. quartz整合spring框架service层对象注入为null解决方案

    Job实现类代码 package cn.itcast.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; imp ...

  7. spring 在service中需要抛出异常才能自动回滚

    在spring 事务配置中,如果service方法捕获了异常,则程序报错也不会自动回滚, 1.手动开启关闭事务 2.抛出异常,可以先捕获异常,然后自定义runtime异常,可不用声明

  8. 案例48-crm练习利用spring管理service和dao层的对象

    1 导包 2 将 Service 对象以及 Dao 对象配置到 spring 容器 <?xml version="1.0" encoding="UTF-8" ...

  9. Java后台代码调用Spring的@Service Bean的方式

    比如:在我的project中有一个类CompassIndexOperation,以: @Service("CompassIndexOperation") @Transactiona ...

  10. 关于Spring注解 @Service @Component @Controller @Repository 用法

    @Component 相当于实例化类的对象,其他三个注解可以理解为@Component的子注解或细化. 在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spr ...

随机推荐

  1. 令人惊叹的Npm工具包

    1.http-server (简单搭建http服务器) 2.json-server (JSON服务器,快速搭建resful api接口) 3.cssnano (css多功能优化工具) PS:比uncs ...

  2. Leetcode 481.神奇字符串

    神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几个元素如下 ...

  3. 解压文件夹python

    # _*_ coding: utf-8 _*_ import zipfile import shutil import os print os.getcwd() basedir = os.path.d ...

  4. c++中set容器的功能及应用。

    set的特性是,所有元素都会根据元素的键值自动排序(默认为升序),set中不允许两个元素有相同的键值. set基本操作: 1.头文件 #include<set>. 注:一定要加上using ...

  5. 一步一步,完成sparkMLlib对日志文件的处理(1)

    https://blog.csdn.net/u012834750/article/details/81014997    初学第一天,当然是完成helloWorld啦,有点艰难,2个小时,在idea, ...

  6. 【HNOI2011/bzoj2337】XOR和路径

    第二道高斯消元练习题 题意 一张无向图,从点 $1$ 出发每次随机选一条出边走,走到 $n$ 停止,求经过的所有边权异或和的期望. $n\le 100$ 题解 注意一点,异或和的期望 $≠$ 期望的异 ...

  7. iOS-BMK标注&覆盖物

    在iOS开发中,地图算是一个比较重要的模块.我们常用的地图有高德地图,百度地图,谷歌地图,对于中国而言,苹果公司已经不再使用谷歌地图,官方使用的是高德地图.下面将讲述一下百度地图开发过程中的一些小的知 ...

  8. 使用 Spring Boot 2.0 + WebFlux 实现 RESTful API

    概述 什么是 Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发框架. 要深入了解 Spring WebFlux, 首先要了知道 R ...

  9. Mondriaan's Dream(poj 2411)

    题意:在n*m的方格里铺1*2的骨牌,有多少种方案 /* 第一次做插头DP,感觉和状压差不多. 这道题是利用上一行的状态来更新下一行的状态. 1代表上一行这个位置填了一个竖的(即本行可以填): 0代表 ...

  10. 洛谷 P 1133 教主的花园

    题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树 ...