《pro Spring》學習筆記之Spring HTTP 遠程方法調用

除了使用Spring集成RMI,AXIS來實現Webservice,Spring也提供了一套自身的類WebService 實現,這就是傳說中《Spring HTTP Invoker》,下面咱們來看如何使用web

一樣,本文分別服務端和客戶端兩個projcetspring

服務端:app

服務接口:測試

package  ch16.SimpleHTTP;
public   interface  HelloWorld  {
    
public String getMessage();
}

服務實現url

package  ch16.SimpleHTTP;
public   class  SimpleHelloWorld  implements  HelloWorld  {
    
public String getMessage() {
        
return "hello world http";
    }

}

使用Spring配置文件發佈一個HTTP調用者服務HttpInvoker-servlet.xml(命名問題後面會有介紹)spa

<? xml version="1.0" encoding="UTF-8" ?>
< beans
    
xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean  id ="defaultHandlerMapping"  class ="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" >
</ bean >

< bean  name ="/helloWorld"  class ="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" >
  
< property  name ="service" >
    
< bean  class ="ch16.SimpleHTTP.SimpleHelloWorld" />
  
</ property >
  
< property  name ="serviceInterface" >
    
< value > ch16.SimpleHTTP.HelloWorld </ value >
  
</ property >
</ bean >
</ beans >

這個配置清晰易懂,但須要注意HttpInvokerServiceExporter被指定爲/helloWorld,當處理請求時候,Spring的DispatcherServlet使用定義了的HandlerMapping去尋找被定義了路由請求的控制器,在這個例子中,咱們定義了一個BeanNameUrlHandlerMapping看成咱們惟一的HandlerMapping的實現,顧名思義,BeanNameUrlHandlerMapping將被用來根據進入的請求來映射到不一樣的URL因此在這個例子中,若是某個進入的請求URI(去掉主機名,上下文路徑,servlet映射後)是/helloWorld,BeanNameHandlerMapping將路它到/helloWorld.net

咱們的配置文件是通ContextLoaderServlet加載,默認的defaultHandlerMapping和上配置文件的內容都是由DispatcherServlet加載,因此,對文件的命名很是重要,須要把文件放到WEB-INF目錄下,而且命名爲<servlet-name-servlet.xm的方式,這裏因爲web.xml中servle名爲httpInvoke,顧命名爲httpInvoker-servlet.xmlxml

web.xml接口

< context-param >
      
< param-name > contextConfigLocation </ param-name >
      
< param-value > /WEB-INF/httpInvoker-servlet.xml </ param-value >
</ context-param >
  
< servlet >
      
< servlet-name > httpInvoker </ servlet-name >
      
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
      
< load-on-startup > 3 </ load-on-startup >
    
</ servlet >
  
   
< servlet-mapping >
    
< servlet-name > httpInvoker </ servlet-name >
    
< url-pattern > /http/* </ url-pattern >
  
</ servlet-mapping >

客戶端:ip

首先把服務端的服務接口打包成jar,放到客戶端的classpath下,或直接copy接口文件

客戶端配置文件:

<? xml version="1.0" encoding="UTF-8" ?>
< beans
    
xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean  id ="helloWorldService"  class ="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" >
  
< property  name ="serviceUrl" >
    
< value > http://localhost:81/ProSpringStudyWeb/http/helloWorld </ value >
  
</ property >
  
< property  name ="serviceInterface" >
    
< value > ch16.SimpleHTTP.HelloWorld </ value >
  
</ property >
</ bean >
</ beans >

測試代碼:

package  ch16.SimpleHTTP;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;


public   class  Test  {

    
public static void main(String[] args) {
        ApplicationContext context 
= new ClassPathXmlApplicationContext("ch16/SimpleHTTP/applicationContext.xml");
        HelloWorld helloWorld
=(HelloWorld)context.getBean("helloWorldService");
        System.out.println(helloWorld.getMessage());
    }


}


運行客戶端後運行測試程序,後臺可打印出:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
hello world http

相關文章
相關標籤/搜索