Spring HTTP Service
基于Spring MVC, 使用Http Service Invoke远程调用方法
(参考: http://blog.csdn.net/hanqunfeng/article/details/4303127)
步骤:
1. 本地定义接口,并在配置文件中说明
PersonService.java
- package com.anialy.httpservice.service;
- import com.anialy.httpservice.entity.Person;
- public interface PersonService {
- public Person getPersonByName(String name);
- }
PersonService.java
- package com.anialy.httpservice.service.impl;
- import com.anialy.httpservice.entity.Person;
- import com.anialy.httpservice.service.PersonService;
- public class PersonServiceImpl implements PersonService{
- public Person getPersonByName(String name) {
- if("anialy".equals(name))
- return new Person("anialy", 100);
- return null;
- }
- }
applicationContext-server-http-service.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- 指定Spring配置文件的Schema信息 -->
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <bean id="httpService"
- class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
- <property name="service">
- <ref bean="personService" />
- </property>
- <property name="serviceInterface" value="com.anialy.httpservice.service.PersonService">
- </property>
- </bean>
- <bean id="personService" class="com.anialy.httpservice.service.impl.PersonServiceImpl"/>
- </beans>
2. mvc配置服务uri与对应的service
applicationContext-mvc.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:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
- 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">
- <mvc:view-controller path="/" view-name="redirect:/home" />
- <mvc:view-controller path="/home" view-name="home" />
- <!-- Spring Service Invoke -->
- <bean
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <props>
- <prop key="/service/httpService">httpService</prop>
- </props>
- </property>
- </bean>
- <!-- Spring MVC -->
- <bean
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <value>
- /test=testController
- </value>
- </property>
- <property name="order" value="1" />
- </bean>
- <bean id="testController" class="com.anialy.webproj.controller.TestController">
- <property name="methodNameResolver" ref="paramResolver" />
- </bean>
- <!-- 定义JSP -->
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/views/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <bean id="paramResolver"
- class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
- <property name="paramName" value="action" />
- <property name="defaultMethodName" value="test" />
- </bean>
- </beans>
3. 客户端配置uri的invoke
applicationContext-client-http-service.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- 指定Spring配置文件的Schema信息 -->
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <bean id="personService"
- class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
- depends-on="propertyConfigurer">
- <property name="serviceUrl" >
- <value>
- http://${host}:${port}/${contextPath}/service/httpService
- </value>
- </property>
- <property name="serviceInterface" value="com.anialy.httpservice.service.PersonService">
- </property>
- </bean>
- </beans>
设置属性文件
system.properties
- serviceName=localhost
- host=127.0.0.1
- port=8080
- contextPath=WebProj
加载配置文件的xml
applicationContext-constants.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:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- <value>classpath:system.properties</value>
- </list>
- </property>
- </bean>
- </beans>
4. 编写测试方法
TestHttpServiceInvoke.java
- package httpservice;
- import java.io.IOException;
- import org.junit.Before;
- import org.junit.Test;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.anialy.httpservice.entity.Person;
- import com.anialy.httpservice.service.PersonService;
- public class TestHttpServiceInvoke {
- private static final Logger logger
- = LoggerFactory.getLogger(TestHttpServiceInvoke.class);
- private ApplicationContext ctx;
- private PersonService personService;
- @Before
- public void init() throws IOException{
- ctx = new ClassPathXmlApplicationContext(new String[]{
- "classpath*:/applicationContext-client-http-service.xml",
- "classpath*:/applicationContext-constants.xml"
- });
- personService = (PersonService) ctx.getBean("personService");
- System.out.println("");
- }
- @Test
- public void test() {
- Person person = personService.getPersonByName("anialy");
- logger.info("姓名:" + person.getName());
- logger.info("年龄:" + person.getAge());
- }
- }
输出:
Spring HTTP Service的更多相关文章
- 整合Spring时Service层为什么不做全局包扫描详解
合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两个 ...
- 测试必须学spring RESTful Service(上)
文末我会说说为什么测试必须学spring. REST REST,是指REpresentational State Transfer,有个精辟的解释什么是RESTful, 看url就知道要什么 看met ...
- spring盒springMVC整合父子容器问题:整合Spring时Service层为什么不做全局包扫描详解
整合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两 ...
- Spring的Service层与Dao层解析
本文转载于网络,觉得写得很透彻. dao完成连接数据库修改删除添加等的实现细节,例如sql语句是怎么写的,怎么把对象放入数据库的.service层是面向功能的,一个个功能模块比如说银行登记并完成一次存 ...
- spring -mvc service层调用工具类配置
在service层时调用工具类时服务返回工具类对象为空 在此工具类上加上@Component注解就可以了 @Component:把普通pojo实例化到spring容器中,相当于配置文件中的 <b ...
- quartz整合spring框架service层对象注入为null解决方案
Job实现类代码 package cn.itcast.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; imp ...
- spring 在service中需要抛出异常才能自动回滚
在spring 事务配置中,如果service方法捕获了异常,则程序报错也不会自动回滚, 1.手动开启关闭事务 2.抛出异常,可以先捕获异常,然后自定义runtime异常,可不用声明
- 案例48-crm练习利用spring管理service和dao层的对象
1 导包 2 将 Service 对象以及 Dao 对象配置到 spring 容器 <?xml version="1.0" encoding="UTF-8" ...
- Java后台代码调用Spring的@Service Bean的方式
比如:在我的project中有一个类CompassIndexOperation,以: @Service("CompassIndexOperation") @Transactiona ...
- 关于Spring注解 @Service @Component @Controller @Repository 用法
@Component 相当于实例化类的对象,其他三个注解可以理解为@Component的子注解或细化. 在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spr ...
随机推荐
- 树状数组 - BZOJ 1103 [POI2007]大都市
bzoj 1103 [POI2007]大都市 描述 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员 Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景. ...
- 理解依赖注入 for Zend framework 2
依赖注入(Dependency Injection),也成为控制反转(Inversion of Control),一种设计模式,其目的是解除类之间的依赖关系. 假设我们需要举办一个Party,Part ...
- 计算n的阶乘(n!)末尾0的个数
题目: 给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数. 举例: 5!=120,其末尾所含有的“0”的个数为1: 10!= 3628800,其末尾所含有的“0”的个数为2: 20!= 24 ...
- custom post types 404 Page Error
问题: 注册新的文章类型后,用新的类型写文章,打开后报 404 错误 原因: 因为虽然注册了新的帖子类型,但WordPress还不知道如何处理它 解决: 到设置 -> 固定链接,重新点击保存,再 ...
- 如何让ie8/ie7/ie6支持html5的<footer></footer><nav></nav>等标签
使用他们能让代码语义化更直观,而且更方便SEO优化.但是此HTML5新标签在IE6/IE7/IE8上并不能识别,需要进行JavaScript处理.以下就介绍几种方式. 方式一:Coding JavaS ...
- [错误解决]刚拿到的服务器vim退格键(backspace)失灵
刚拿到的服务器vim退格键(backspace)失灵: 解决方案: 在主目录下建立.vimrc 覆盖/etc/vimrc的配置 .vimrc 与 /etc/vimrc的区别: 在启动的时候vim会读取 ...
- Python杂技
py转exe文件 用 pyinstaller,可以把所有文件打包成一个单独的exe文件 win10X64 =>pip install pyinstaller pyinstaller [参数] [ ...
- sysctl内核参数解析
sysctl内核参数解析 kernel.参数 kernel.shmall = 2097152 ## 1> 表示所有内存大小.可以分配的所有共享内存段的总和最大值.(以页为单位) ## 2& ...
- SQLSERVER中文日期varchar格式转换成datetime格式
因项目要求,需要把SQLSERVER一张客户表的数据同步到oracle库的一张客户表,但两张表有时间类型不一致,需要进行转换 如下: SELECT CUSTCODE,AgreementValidity ...
- [luoguP2336] [SCOI2012]喵星球上的点名(后缀数组 + 暴力)
传送门 原本的想法是把所有的串不管是名字还是询问都连起来,记录一下询问串在sa数组中的位置 对于每个询问可以在sa数组中二分出左右边界,第一问用莫队,第二问差分乱搞. 结果发现我差分的思路想错了,先写 ...