【一】spring的遠端調用提供的基礎類
(1)org.springframework.remoting.support.RemotingSupport
===>spring提供實作的遠端調用用戶端實作的基礎類
===>例子:org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean
org.springframework.remoting.caucho.HessianProxyFactoryBean
(2)org.springframework.remoting.support.RemoteExporter
===>spring提供實作的遠端調用服務端實作的基礎類
===>例子:org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter
org.springframework.remoting.caucho.HessianServiceExporter
【二】spring的遠端調用基于Http協定實作的封裝,以該例子分析遠端調用實作原理和源碼分析
(1)HttpInvokerProxyFactoryBean 用戶端的實作
===>HttpInvokerProxyFactoryBean類是一個FactoryBean的實作接口,注入IOC後,未來向IOC申請bean,其實傳回的是getObject()方法傳回的實作.在執行個體化階段會調用afterPropertiesSet() 進行初始化.根據配置建立代理對象.
===>在getObject()方法中,是完成了一個代理對象的封裝.代理增強的配置:serviceUrl和serviceInterface.一個配置的請求url,一個配置的要代理的接口.
===>該代理對象的增強實作就是org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor的invoke(MethodInvocation methodInvocation) 方法.也就是HttpInvokerProxyFactoryBean的父類.将來會作為增強實作,加入到代理對象中.
===>未來發起調用.其實底層是代理對象的攔截器,也就是HttpInvokerClientInterceptor調用invoke方法.将資料類序列化,利用serviceUrl向遠端調用接口發送http請求.
(2)HttpInvokerServiceExporter 服務端的實作
===>HttpInvokerServiceExporter類是org.springframework.web.HttpRequestHandler的實作類.該類在執行個體化的時候,會調用afterPropertiesSet()初始化.如果配置有攔截器(即屬性Object[] interceptors),則需要為實際調用的facadeImpl建立代理對象.
===>該類将來會作為一個bean加入到org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping中.
===>當用戶端發送請求,進入DispatcherServlet中,從beanNameUrlHandlerMapping中擷取該bean,再用該bean找到org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.該HttpRequestHandlerAdapter會判斷bean是否是HttpRequestHandler的實作類的執行個體.如果是,調用HttpInvokerServiceExporter類的handleRequest()方法去實作真正facadeImpl的調用
===>在配置HttpInvokerServiceExporter的時候的配置
>需要配置url,将來作為和用戶端的請求位址的比對.
>需要配置所代理的接口.
>需要配置真正的實作類的執行個體,該執行個體也可能在執行個體化bean的時候如果有aop配置,其實也是一個代理對象.這就是多層代理.多層代理在技術層面是允許的.
(3)BeanNameUrlHandlerMapping,HttpRequestHandlerAdapter是如何加載進IOC容器中的?
===>初始化DispatcherServlet的時候,最後會調用initStrategies(ApplicationContext context)方法,内部有初始化的方法,從配置檔案裡加載,然後編碼方式加入IOC容器.從DispatcherServlet.properties檔案中擷取相應的配置.
===>BeanNameUrlHandlerMapping是ApplicationContextAware 接口的實作類.在IOC容器執行個體化階段,會調用setApplicationContext(ApplicationContext context)進行映射配置.
【三】以HttpInvoker為例子寫一個服務端和用戶端
(1)facade接口

View Code
(2)facade接口的參數

(3)facade實作

(4)用戶端配置實作

(5)服務端配置實作
