Spring HTTP Service
来源:互联网 发布:问卷数据分析 编辑:程序博客网 时间:2024/06/08 00:18
简述:
基于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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://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 --><beanclass="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 --><beanclass="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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://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=localhosthost=127.0.0.1port=8080contextPath=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.xsdhttp://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;@Beforepublic 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("");}@Testpublic void test() {Person person = personService.getPersonByName("anialy");logger.info("姓名:" + person.getName());logger.info("年龄:" + person.getAge());}}
输出:
0 0
- Spring HTTP Service
- http mock service
- icinga 添加http service
- Apache Felix HTTP Service
- AngularJS $http Service
- Http Invoker Service & Client
- AngularJS $http service
- HTTP Service服务去哪啦?
- http 503 service
- 15-service之$http
- Spring Service Application Context
- spring管理service类
- junit4测试service-spring
- Spring Web Service
- spring service &DAO
- spring @resource @service 区别
- axis2+spring 发布service
- spring service事务传播
- 亲测 S3C2440 dnw下载驱动 win7_x64
- Python 多线程
- Linux系统性能分析工具-操作篇
- java深度探究---Map常见用法,遍历
- SharePoint 2010 PowerShell 系列 之 备份、还原、部署 .WSP
- Spring HTTP Service
- ActiveMQ在JMS中的运用-深入浅出JMS
- 数据库连接池libzdb.2.12在Centos 6.5 64位下的安装
- git 命令大全
- Oracle_Day2 子查询
- 超实用的JavaScript技巧及最佳实践(下)
- 黑马程序员-集合体系汇总
- java学习——instanceof
- 处理某客户p570硬盘故障所思