基于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. }


输出:


最新文章

  1. nohup
  2. Android Studio快捷键每日一练(5)
  3. 前端---HTML
  4. C#获取相对路径
  5. 【Android】 PopupWindow使用小结
  6. [转]log4net使用(WinForm/WebFrom)
  7. linux命令补全 忘记命令只记得开头
  8. C++中四种类型转换方式
  9. MySQL学习笔记:MySQL: ERROR 1064(42000)
  10. MSSQL 日期操作函数 总结
  11. linux系统下手动安装Angular-cli
  12. 前端优化之动画为什么要尽量用css3代替js
  13. 事务回滚 try catch
  14. [JavaScript]iframe的contentWindow
  15. 关于js的函数
  16. LInux下(centos7.2)更新 python3.7
  17. Docker 微服务教程
  18. java 向上转型和向下转型
  19. CloseableHttpClient与 CloseableHttpResponse应用
  20. TopJUI Combobox onSelect 事件失效BUG

热门文章

  1. Apache简易快速安装
  2. Python 实战一
  3. 带有命名空间的xml解析,C#
  4. PHP-7.1 源代码学习:字节码生成 之 &quot;$a = 1&quot;
  5. 九度oj 题目1411:转圈
  6. [图论训练]BZOJ 3245: 最快路线【最短路】
  7. cf524C The Art of Dealing with ATM
  8. hdu 4671 异面直线的距离
  9. 37深入理解C指针之---结构体与指针
  10. du 查看 資料夾 佔用空間