zoukankan      html  css  js  c++  java
  • Spring HTTP Service

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

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


    步骤:

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

    PersonService.java

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


    PersonService.java

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


    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.   
    13.     <bean id="httpService"  
    14.         class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
    15.         <property name="service">  
    16.             <ref bean="personService" />  
    17.         </property>  
    18.         <property name="serviceInterface" value="com.anialy.httpservice.service.PersonService">  
    19.         </property>  
    20.     </bean>  
    21.       
    22.     <bean id="personService" class="com.anialy.httpservice.service.impl.PersonServiceImpl"/>  
    23.       
    24.   
    25.       
    26. </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.   
    9.     <mvc:view-controller path="/" view-name="redirect:/home" />  
    10.     <mvc:view-controller path="/home" view-name="home" />  
    11.   
    12.     <!-- Spring Service Invoke -->  
    13.     <bean  
    14.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
    15.          <property name="mappings">  
    16.             <props>  
    17.                   <prop key="/service/httpService">httpService</prop>  
    18.             </props>  
    19.         </property>  
    20.     </bean>  
    21.   
    22.   
    23.     <!-- Spring MVC -->  
    24.     <bean  
    25.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
    26.         <property name="mappings">  
    27.             <value>  
    28.                 /test=testController  
    29.             </value>  
    30.         </property>  
    31.         <property name="order" value="1" />  
    32.     </bean>  
    33.   
    34.     <bean id="testController" class="com.anialy.webproj.controller.TestController">  
    35.         <property name="methodNameResolver" ref="paramResolver" />  
    36.     </bean>  
    37.   
    38.     <!-- 定义JSP -->  
    39.     <bean  
    40.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    41.         <property name="prefix" value="/WEB-INF/views/" />  
    42.         <property name="suffix" value=".jsp" />  
    43.     </bean>  
    44.   
    45.     <bean id="paramResolver"  
    46.         class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">  
    47.         <property name="paramName" value="action" />  
    48.         <property name="defaultMethodName" value="test" />  
    49.     </bean>  
    50. </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.   
    13.     <bean id="personService"  
    14.         class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"  
    15.         depends-on="propertyConfigurer">  
    16.         <property name="serviceUrl" >  
    17.             <value>  
    18.                 http://${host}:${port}/${contextPath}/service/httpService  
    19.             </value>  
    20.         </property>  
    21.         <property name="serviceInterface" value="com.anialy.httpservice.service.PersonService">  
    22.         </property>  
    23.     </bean>  
    24.   
    25. </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.   
    3. import java.io.IOException;  
    4.   
    5. import org.junit.Before;  
    6. import org.junit.Test;  
    7. import org.slf4j.Logger;  
    8. import org.slf4j.LoggerFactory;  
    9. import org.springframework.context.ApplicationContext;  
    10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    11.   
    12. import com.anialy.httpservice.entity.Person;  
    13. import com.anialy.httpservice.service.PersonService;  
    14.   
    15. public class TestHttpServiceInvoke {  
    16.     private static final Logger logger   
    17.         = LoggerFactory.getLogger(TestHttpServiceInvoke.class);  
    18.       
    19.     private ApplicationContext ctx;  
    20.     private PersonService personService;  
    21.   
    22.     @Before  
    23.     public void init() throws IOException{  
    24.         ctx = new ClassPathXmlApplicationContext(new String[]{  
    25.                 "classpath*:/applicationContext-client-http-service.xml",  
    26.                 "classpath*:/applicationContext-constants.xml"  
    27.         });  
    28.           
    29.         personService = (PersonService)  ctx.getBean("personService");  
    30.         System.out.println("");  
    31.     }  
    32.       
    33.       
    34.     @Test  
    35.     public void test() {  
    36.         Person person = personService.getPersonByName("anialy");  
    37.         logger.info("姓名:" + person.getName());  
    38.         logger.info("年龄:" + person.getAge());  
    39.     }  
    40. }  



    输出:




  • 相关阅读:
    安装VMtools vim编辑器的使用 压缩包命令 Linux下的用户管理 (第三天)
    VM虚拟机安装 常用Linux命令 网卡配置 (第二天)
    数据库的交互模式 常用的dos命令 (第一天)
    Validate US Telephone Numbers FreeCodeCamp
    Arguments Optional FreeCodeCamp
    Everything Be True FreeCodeCamp
    Binary Agents FreeCodeCamp
    Steamroller FreeCodeCamp
    Drop it FreeCodeCamp
    Smallest Common Multiple FreeCodeCamp
  • 原文地址:https://www.cnblogs.com/xijin-wu/p/7541829.html
Copyright © 2011-2022 走看看